输入一行字符,分别统计其中的英文字母,空格,数字和其他字符的个数用c语言写
时间: 2023-06-08 13:07:14 浏览: 52
好的,我将会按您的要求回答您的问题。以下是一个简单的 C 代码示例,可以输入一行字符并统计其中的英文字母,空格,数字和其他字符的个数:
```c
#include<stdio.h>
#include<ctype.h>
int main() {
char input[100];
int letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入一行字符:\n");
fgets(input, sizeof(input), stdin);
for(int i = 0; input[i]; i++) {
if(isalpha(input[i])) {
letters++;
}
else if(isspace(input[i])) {
spaces++;
}
else if(isdigit(input[i])) {
digits++;
}
else {
others++;
}
}
printf("输入的字符中包含 %d 个英文字母,%d 个空格,%d 个数字和 %d 个其他字符。\n", letters, spaces, digits, others);
return 0;
}
```
这个程序使用了 C 标准库中的 isalpha、isspace 和 isdigit 函数来判断一个字符是否为英文字母、空格或数字,然后遍历输入字符串并根据每个字符的类型来更新相应计数器的值。最终输出结果包括英文字母、空格、数字和其他字符的个数。