指针处置字符串
指针处理字符串
目的:
输入一行文字,找出其中大写字母,小写字母,数字,空格及其他字符各有多少
代码:
int main ()
{
int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;
char *string = (char*) malloc(sizeof(char));
printf("请输入字符串:\n");
gets(string);
char *p = string;
while(*p != '\n')
{
if(('A' <= *p) && (*p <= 'Z'))
++upper;
else
if(('a' <= *p) && (*p <= 'z'))
++lower;
else
if(('0' <= *p) && (*p <= '9'))
++digit;
else
if(*p == ' ')
++space;
else
++other;
p++;
}
printf("大写字母: %d, 小写字母: %d, ",upper,lower);
printf("数字: %d, 空格: %d\n, 其他字符: %d\n",digit,space,other);
return 0;
}
怎样修改,才能达到指定效果?
------解决方案--------------------
目的:
输入一行文字,找出其中大写字母,小写字母,数字,空格及其他字符各有多少
代码:
int main ()
{
int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;
char *string = (char*) malloc(sizeof(char));
printf("请输入字符串:\n");
gets(string);
char *p = string;
while(*p != '\n')
{
if(('A' <= *p) && (*p <= 'Z'))
++upper;
else
if(('a' <= *p) && (*p <= 'z'))
++lower;
else
if(('0' <= *p) && (*p <= '9'))
++digit;
else
if(*p == ' ')
++space;
else
++other;
p++;
}
printf("大写字母: %d, 小写字母: %d, ",upper,lower);
printf("数字: %d, 空格: %d\n, 其他字符: %d\n",digit,space,other);
return 0;
}
怎样修改,才能达到指定效果?
------解决方案--------------------
- C/C++ code
#include<stdio.h> int main () { int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0; char string[100]; char *p; printf("请输入字符串:\n"); gets(string); p = string; while(*p != '\0') { if(('A' <= *p) && (*p <= 'Z')) ++upper; else if(('a' <= *p) && (*p <= 'z')) ++lower; else if(('0' <= *p) && (*p <= '9')) ++digit; else if(*p == ' ') ++space; else ++other; p++; } printf("大写字母: %d, 小写字母: %d, ",upper,lower); printf("数字: %d, 空格: %d\n, 其他字符: %d\n",digit,space,other); return 0; }
------解决方案--------------------
将while(*p !='\n')
改为while(*p !='\0')试试
------解决方案--------------------
楼主又消失了 程序好没?
------解决方案--------------------
你把where(*p != '\n')改成where(*p != NULL)试试!!
C标准里规定malloc必须指定大小,你用(char*) malloc(sizeof(char))实际上只分配了4字节的空间给string。
如果你运气好的话:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;
char *string = (char*) malloc(sizeof(char));
printf("请输入字符串:\n");
gets(string);
char *p = string;
while(*p != NULL)
{
if(('A' <= *p) && (*p <= 'Z'))
++upper;
else if(('a' <= *p) && (*p <= 'z'))
++lower;
else if(('0' <= *p) && (*p <= '9'))
++digit;
else if(*p == ' ')
++space;
else
++other;
p++;
}
printf("大写字母: %d, 小写字母: %d, ",upper, lower);
printf("数字: %d, 空格: %d\n, 其他字符: %d\n", digit,space,other);
return 0;
}
能运行,但如果是在poj上做题肯定Wrong Answer。
正规的写法你可以这样:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 500 // 这个值你自己设定
int main(int argc, char* argv[])
{
int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;
char *string = (char*) malloc(SIZE * sizeof(char));
printf("请输入字符串:\n");
gets(string);
char *p = string;
while(*p != NULL)
{
if(('A' <= *p) && (*p <= 'Z'))
++upper;
else if(('a' <= *p) && (*p <= 'z'))
++lower;
else if(('0' <= *p) && (*p <= '9'))
++digit;
else if(*p == ' ')
++space;
else
++other;
p++;
}
printf("大写字母: %d, 小写字母: %d, ",upper, lower);
printf("数字: %d, 空格: %d\n, 其他字符: %d\n", digit,space,other);
return 0;
}
不知道能不能解答好你问题。
------解决方案--------------------
#include <stdio.h>
#include <ctype.h>
#define MAX 100
int main ()
{
int upper = 0, lower = 0, digit = 0, space = 0, other = 0, num = 0;