输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。C语言
时间: 2023-07-10 09:19:59 浏览: 50
下面是一段 C 语言代码,用于统计输入字符串中的英文字母、数字、空格和其他字符的个数:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
int letter = 0, digit = 0, space = 0, others = 0;
printf("请输入一行字符:\n");
while ((c = getchar()) != '\n') {
if (isalpha(c)) {
letter++; // 统计英文字母
} else if (isdigit(c)) {
digit++; // 统计数字
} else if (isspace(c)) {
space++; // 统计空格
} else {
others++; // 统计其他字符
}
}
printf("输入字符中共有:\n");
printf("英文字母:%d\n", letter);
printf("数字:%d\n", digit);
printf("空格:%d\n", space);
printf("其他字符:%d\n", others);
return 0;
}
```
程序会提示用户输入一行字符,然后使用 `getchar()` 函数从标准输入读取每个字符,并使用 `isalpha()`、`isdigit()` 和 `isspace()` 函数判断字符类型,最后统计各种类型字符的个数并输出。