c语言输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。代码
时间: 2023-11-25 20:03:54 浏览: 27
```c
#include <stdio.h>
int main() {
char c;
int letter=0, space=0, digit=0, other=0;
printf("请输入一行字符:\n");
while ((c = getchar()) != '\n') {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
letter++;
else if (c == ' ')
space++;
else if (c >= '0' && c <= '9')
digit++;
else
other++;
}
printf("该行字符中英文字母:%d,空格:%d,数字:%d,其他字符:%d", letter, space, digit, other);
return 0;
}
```
说明:
1. 声明变量 `letter`、`space`、`digit`、`other` 分别用于统计英文字母、空格、数字和其他字符的个数。
2. 在 `while` 循环中,每次读入一个字符 `c`,如果是字母,`letter` 变量加 1;如果是空格,`space` 变量加 1;如果是数字,`digit` 变量加 1;否则,`other` 变量加 1。
3. 循环读入直到读入换行符 `\n`,即读入一行字符结束。
4. 最后输出统计结果。