C语言输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
时间: 2023-11-25 07:58:38 浏览: 59
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, alpha_count = 0, digit_count = 0, space_count = 0, other_count = 0;
printf("Enter a string: ");
fgets(str, 100, stdin);
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
alpha_count++;
} else if (isdigit(str[i])) {
digit_count++;
} else if (isspace(str[i])) {
space_count++;
} else {
other_count++;
}
}
printf("Number of alphabets: %d\n", alpha_count);
printf("Number of digits: %d\n", digit_count);
printf("Number of spaces: %d\n", space_count);
printf("Number of other characters: %d\n", other_count);
return 0;
}
```
阅读全文