c语言分别统计出输入的英文字母、数字、空格和其他符号的个数
时间: 2023-09-24 10:10:49 浏览: 70
下面是一个简单的 C 语言程序,可以统计输入中的英文字母、数字、空格和其他符号的个数:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
int ch;
int letters = 0, digits = 0, spaces = 0, others = 0;
printf("请输入一串字符:\n");
while ((ch = getchar()) != EOF) {
if (isalpha(ch)) {
letters++;
} else if (isdigit(ch)) {
digits++;
} else if (isspace(ch)) {
spaces++;
} else {
others++;
}
}
printf("英文字母个数:%d\n", letters);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他符号个数:%d\n", others);
return 0;
}
```
该程序使用 `getchar()` 函数从标准输入读取字符,然后使用 `isalpha()`、`isdigit()` 和 `isspace()` 函数判断字符的类型,并分别统计不同类型字符的个数。最后将结果输出。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)