用C语言实现输入一行字符,分别统计出其中的英文字母、空格、数字和其他字符的个数。
时间: 2023-03-01 22:03:22 浏览: 109
这是 C 语言实现统计字符串中字符种类数量的代码:
```
#include <stdio.h>
#include <ctype.h>
int main()
{
int letter_count = 0, space_count = 0, digit_count = 0, other_count = 0;
char ch;
printf("请输入一个字符串:");
while ((ch = getchar()) != '\n') {
if (isalpha(ch)) {
letter_count++;
} else if (isspace(ch)) {
space_count++;
} else if (isdigit(ch)) {
digit_count++;
} else {
other_count++;
}
}
printf("字母数:%d\n", letter_count);
printf("空格数:%d\n", space_count);
printf("数字数:%d\n", digit_count);
printf("其他字符数:%d\n", other_count);
return 0;
}
```
在这段代码中,我们使用了 `getchar` 函数读取字符,并使用了 `isalpha`,`isspace` 和 `isdigit` 函数判断读取的字符是否为字母,空格,数字,还是其他字符。最后,将统计的结果输出。
阅读全文
相关推荐















