C输入一个字符串统计大写字母,小写字母,数字字符,空格及其他字符个数
时间: 2023-07-01 17:04:46 浏览: 143
可以使用C语言中的字符处理函数和循环语句来实现统计不同类型字符个数的功能。具体实现代码如下:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, upper_count = 0, lower_count = 0, digit_count = 0, space_count = 0, other_count = 0;
printf("请输入一个字符串: ");
fgets(str, 100, stdin);
for(i = 0; str[i] != '\0'; i++) {
if(isupper(str[i])) {
upper_count++;
}
else if(islower(str[i])) {
lower_count++;
}
else if(isdigit(str[i])) {
digit_count++;
}
else if(str[i] == ' ') {
space_count++;
}
else {
other_count++;
}
}
printf("大写字母个数: %d\n", upper_count);
printf("小写字母个数: %d\n", lower_count);
printf("数字字符个数: %d\n", digit_count);
printf("空格个数: %d\n", space_count);
printf("其他字符个数: %d\n", other_count);
return 0;
}
```
程序首先提示用户输入一个字符串,然后使用fgets函数从标准输入中读取字符串,保存在字符数组str中。接着使用循环语句遍历字符数组,对于每个字符,使用字符处理函数判断它是大写字母、小写字母、数字字符、空格还是其他字符,并累加对应类型字符的个数。最后输出统计结果。