C语言 从键盘输入一行英文文字,不超过80个字符,分别统计出其中英文大写字母、小写字母、数字、空格、及其它字符的个数。 (利用字符数组下标访问数组中每个字符)。
时间: 2024-05-10 13:16:29 浏览: 91
下面是 C 语言的代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[81];
int i, upper=0, lower=0, digit=0, space=0, other=0;
printf("请输入一行英文文字(不超过80个字符):\n");
fgets(str, 81, stdin); // 从键盘读入一行字符串,最大长度为 81(包括 '\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;
}
```
代码的核心部分是 for 循环,遍历输入的字符串,对每个字符进行判断。其中,`isupper()`、`islower()`、`isdigit()` 和 `isspace()` 是 C 语言中的字符函数,用于判断一个字符是否为大写字母、小写字母、数字或空格。如果不是这些字符中的任何一种,就认为是其他字符。最后输出各种字符出现的个数即可。
阅读全文
相关推荐















