请用C语言写输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数的代码
时间: 2023-06-04 09:08:35 浏览: 70
我可以帮您编写这段代码。以下是使用C语言编写输入一行字符,统计其中英文字母、空格、数字和其他字符个数的示例代码:
```
#include <stdio.h>
int main() {
char str[100];
int i, letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入一行字符:");
fgets(str, 100, stdin);
for (i = 0; str[i] != '\0'; i++) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
letters++;
} else if (str[i] == ' ') {
spaces++;
} else if (str[i] >= '0' && str[i] <= '9') {
digits++;
} else {
others++;
}
}
printf("其中英文字母个数为:%d\n", letters);
printf("其中空格个数为:%d\n", spaces);
printf("其中数字个数为:%d\n", digits);
printf("其中其他字符个数为:%d\n", others);
return 0;
}
```
这段代码可以接受用户从键盘输入一行字符,然后使用循环遍历每个字符,并统计其中英文字母、空格、数字和其他字符的个数,最终输出结果。