用C++编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。
时间: 2023-11-17 10:58:10 浏览: 74
```c
#include <stdio.h>
#include <ctype.h>
void count(char *str, int *letter, int *digit, int *space, int *other) {
while (*str) {
if (isalpha(*str)) {
(*letter)++;
} else if (isdigit(*str)) {
(*digit)++;
} else if (isspace(*str)) {
(*space)++;
} else {
(*other)++;
}
str++;
}
}
int main() {
char str[100];
int letter = 0, digit = 0, space = 0, other = 0;
printf("Enter a string: ");
fgets(str, 100, stdin);
count(str, &letter, &digit, &space, &other);
printf("The string contains:\n");
printf("%d letters\n", letter);
printf("%d digits\n", digit);
printf("%d spaces\n", space);
printf("%d other characters\n", other);
return 0;
}
```
阅读全文