C语言编程输入一行字符分别统计出其中英文字母,空格,数字和其他字符的个数
时间: 2023-11-07 12:00:54 浏览: 66
好的,我可以回答这个问题。请看下面的代码:
```
#include <stdio.h>
#include <ctype.h>
int main() {
char input[100];
int letters = 0;
int spaces = 0;
int digits = 0;
int others = 0;
printf("请输入一行字符:\n");
fgets(input, 100, stdin);
for (int i = 0; input[i] != '\0'; i++) {
if (isalpha(input[i])) {
letters++;
} else if (isspace(input[i])) {
spaces++;
} else if (isdigit(input[i])) {
digits++;
} else {
others++;
}
}
printf("字符数:%d\n英文字母数:%d\n空格数:%d\n数字数:%d\n其他字符数:%d\n", letters + spaces + digits + others, letters, spaces, digits, others);
return 0;
}
```
这个程序使用了 `fgets()` 函数来读取一行字符,然后使用 `isalpha()`、`isspace()` 和 `isdigit()` 函数来判断每个字符的类型。最后统计出各种类型字符的数量并输出。