在c语言中统计一个字符串中字母,空格,数字和其他字符的个数,主函数负责输入和输出。
时间: 2024-03-12 18:44:13 浏览: 25
好的,这里是一个示例代码,可以实现你所需的功能:
```c
#include <stdio.h>
#include <ctype.h>
void count_chars(char *str, int *letters, int *spaces, int *digits, int *others) {
while(*str) {
if(isalpha(*str)) {
(*letters)++;
} else if(isspace(*str)) {
(*spaces)++;
} else if(isdigit(*str)) {
(*digits)++;
} else {
(*others)++;
}
str++;
}
}
int main() {
char str[100];
int letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入一个字符串:\n");
fgets(str, 100, stdin);
count_chars(str, &letters, &spaces, &digits, &others);
printf("字母个数:%d\n", letters);
printf("空格个数:%d\n", spaces);
printf("数字个数:%d\n", digits);
printf("其他字符个数:%d\n", others);
return 0;
}
```
在这个程序中,`count_chars()` 函数用于统计一个字符串中字母,空格,数字和其他字符的个数。`main()` 函数负责输入和输出。当你输入一个字符串时,程序将会输出该字符串中字母,空格,数字和其他字符的个数。
希望对你有帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)