用c语言写出:有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。
时间: 2023-11-28 19:08:50 浏览: 56
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char text[3][81]; // 用二维数组存储3行文本,每行最多80个字符
int i, j, upper = 0, lower = 0, digit = 0, space = 0, other = 0;
// 读入3行文本
for (i = 0; i < 3; i++) {
printf("请输入第%d行文本:\n", i + 1);
fgets(text[i], 81, stdin); // 使用fgets读入一行文本,最多读入80个字符
}
// 统计各类字符个数
for (i = 0; i < 3; i++) {
for (j = 0; j < 80 && text[i][j] != '\0'; j++) {
if (isupper(text[i][j])) {
upper++;
} else if (islower(text[i][j])) {
lower++;
} else if (isdigit(text[i][j])) {
digit++;
} else if (isspace(text[i][j])) {
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;
}
```
注意,这里使用了`fgets`函数读入一行文本,而不是`scanf`函数。因为`scanf`函数在读取字符串时容易遇到空格、换行等问题,不如`fgets`函数方便。