有一篇文章总共有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。用c++程序编写
时间: 2023-05-21 17:07:26 浏览: 67
以下是 CSDN 开发的 C 知道的回答:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[80];
int i, upper, lower, digit, space, other;
printf("请输入一段文字(不超过80个字符):\n");
fgets(str, 80, stdin);
upper = lower = digit = space = other = 0; // 初始化计数器
for (i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
upper++;
} else if (islower(str[i])) {
lower++;
} else if (isdigit(str[i])) {
digit++;
} else if (isspace(str[i])) {
space++;
} else {
other++;
}
}
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("数字:%d\n", digit);
printf("空格:%d\n", space);
printf("其他字符:%d\n", other);
return 0;
}
```
运行该程序后,在屏幕上输入一段不超过 80 个字符的文本,程序会输出该文本中各种字符的个数。例如输入:
```
Hello 123 World! 中文加密 lua closure factory 完整代码
```
程序输出:
```
大写字母:3
小写字母:11
数字:3
空格:6
其他字符:15
```