用C语言输入一行字符,统计英文字母、空格、数字和其他字符的个数。
时间: 2023-05-28 14:08:04 浏览: 98
C语言程序设计-输入一行字符,分别统计出英文大小写字母、空格、数字和其他字符的个数
5星 · 资源好评率100%
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int letters = 0, spaces = 0, digits = 0, others = 0;
printf("Please input a string: ");
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
letters++;
} else if (isspace(str[i])) {
spaces++;
} else if (isdigit(str[i])) {
digits++;
} else {
others++;
}
}
printf("Number of letters: %d\n", letters);
printf("Number of spaces: %d\n", spaces);
printf("Number of digits: %d\n", digits);
printf("Number of other characters: %d\n", others);
return 0;
}
```
阅读全文