有一段文字,共有5行,分别统计其中的大写英文字母、小写英文字母、数字、空格及其他字符的个数,并输出结果。用c语言写
时间: 2025-01-26 14:06:30 浏览: 37
在C语言中,你可以使用`ctype.h`库中的函数来处理各种字符类型,然后遍历字符串逐一计数。以下是一个简单的示例程序,它接受用户输入的一段文本,并计算大写字母、小写字母、数字和空格的数量:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char input[60];
int uppercase = 0;
int lowercase = 0;
int digits = 0;
int spaces = 0;
int other_chars = 0;
printf("请输入一段文本 (不超过50个字符): ");
fgets(input, sizeof(input), stdin);
// 将换行符从输入末尾移除
input[strlen(input) - 1] = '\0';
for(int i = 0; input[i]; i++) { // 遍历字符串
if(isupper(input[i])) { // 大写字母
uppercase++;
} else if(islower(input[i])) { // 小写字母
lowercase++;
} else if(isdigit(input[i])) { // 数字
digits++;
} else if isspace(input[i])) { // 空格
spaces++;
} else { // 其他字符
other_chars++;
}
}
printf("大写字母: %d\n", uppercase);
printf("小写字母: %d\n", lowercase);
printf("数字: %d\n", digits);
printf("空格: %d\n", spaces);
printf("其他字符: %d\n", other_chars);
return 0;
}
```
这个程序会读取用户输入的一段文本,然后对每个字符进行检查并计数。注意,这里假设了输入不会超过数组`input`的大小,实际应用中你需要处理超出长度的情况。
阅读全文
相关推荐




















