有一篇短文,共有3行文字,每行有80个字符。想统计出其中英文大写字母,小写字母,数字、空格以及其他字符各有多少个(用c语言)
时间: 2023-06-19 22:07:11 浏览: 70
可以使用C语言的字符处理函数和循环来实现这个统计功能。以下是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char text[3][80] = {"This is line 1.", "Line 2 has 123 numbers.", "Line 3 has special characters: !@#$%^&*()_+"};
int i, j;
int uppercase = 0, lowercase = 0, digits = 0, spaces = 0, others = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 80 && text[i][j] != '\0'; j++) {
if (isupper(text[i][j])) {
uppercase++;
} else if (islower(text[i][j])) {
lowercase++;
} else if (isdigit(text[i][j])) {
digits++;
} else if (isspace(text[i][j])) {
spaces++;
} else {
others++;
}
}
}
printf("Uppercase letters: %d\n", uppercase);
printf("Lowercase letters: %d\n", lowercase);
printf("Digits: %d\n", digits);
printf("Spaces: %d\n", spaces);
printf("Other characters: %d\n", others);
return 0;
}
```
这个程序使用了一个字符数组来存储文本的每一行。然后使用两个循环来遍历每个字符,并使用`isupper()`、`islower()`、`isdigit()`和`isspace()`函数来判断每个字符的类型。最后,将计数器的值打印到屏幕上。