代码生成c语言统计字符串中英文字母,空格,数字和其他字母的个数
时间: 2023-05-28 12:06:25 浏览: 102
以下是一个简单的代码示例,用于在C语言中统计字符串中的字母,空格,数字和其他字符的数量:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, alphabets, digits, spaces, others;
alphabets = digits = spaces = others = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for(i=0; str[i]!='\0'; i++) {
if(isalpha(str[i])) {
alphabets++;
}
else if(isdigit(str[i])) {
digits++;
}
else if(isspace(str[i])) {
spaces++;
}
else {
others++;
}
}
printf("Total Alphabets = %d\n", alphabets);
printf("Total Digits = %d\n", digits);
printf("Total Spaces = %d\n", spaces);
printf("Total Other Characters = %d\n", others);
return 0;
}
```
运行示例:
```
Enter a string: Hello World! 123
Total Alphabets = 10
Total Digits = 3
Total Spaces = 2
Total Other Characters = 1
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](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)