用c语言编程实现:输入一行字符串,放入数组str中,分别统计出str其中的英文大写字母、小写字母、空格、数字和其它字符的个数。
时间: 2024-12-10 16:36:10 浏览: 70
在C语言中,你可以通过遍历字符串并利用ASCII码值来实现这个功能。这里是一个简单的示例代码:
```c
#include <stdio.h>
#include <ctype.h>
#define MAX_CHAR 100
int main() {
char str[MAX_CHAR];
int uppercase = 0, lowercase = 0, spaces = 0, digits = 0, others = 0;
printf("请输入一行字符串:");
fgets(str, MAX_CHAR, stdin); // 读取用户输入,包括换行符
for (int i = 0; str[i] != '\0'; i++) { // 遍历字符串
if (isupper(str[i])) { // 判断是否为大写字母
uppercase++;
} else if (islower(str[i])) { // 判断是否为小写字母
lowercase++;
} else if (isspace(str[i])) { // 判断是否为空格
spaces++;
} else if (isdigit(str[i])) { // 判断是否为数字
digits++;
} else { // 其他字符(如特殊字符)
others++;
}
}
printf("\n统计结果:\n");
printf("大写字母:%d\n", uppercase);
printf("小写字母:%d\n", lowercase);
printf("空格:%d\n", spaces);
printf("数字:%d\n", digits);
printf("其他字符:%d\n", others);
return 0;
}
```
当你运行此程序时,它会提示你输入一行字符串,然后计算并显示各种字符类型的计数。
阅读全文
相关推荐


















