P292 8.输入一行字符串,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少
#include<stdio.h>
int main() {
int upp = 0, low = 0, space = 0, digit = 0, other = 0;
char str[100] = {0};
char* p = str;
printf("请输入一行字符串:");
gets(str);
p = &str[0];
while (*p != '\0') {
if ((*p >= 'A') && (*p <= 'Z'))
upp++;
else if ((*p >= 'a') && (*p <= 'z'))
low++;
else if ((*p >= '0') && (*p <= '9'))
digit++;
else if (*p == ' ')
space++;
else
other++;
p++;
}
printf("该字符串中的大写字母数为:%d 该字符串中的小写字母数为:%d\n", upp, low);
printf("该字符串中的数字数为:%d 该字符串中的空格数为:%d\n", digit, space);
printf("该字符串中的其他字符数为:%d", other);
return 0;
}
运行结果: