c语言输入一行字符分别统计其中英文字母 空格 数字 和其他字符的个数
时间: 2023-10-26 18:17:05 浏览: 146
好的,我知道了。以下是 c 语言统计字符的代码:
```
#include <stdio.h>
#include <ctype.h>
int main() {
char input;
int letters = 0, digits = 0, spaces = 0, others = 0;
printf("请输入一行字符:\n");
while ((input = getchar()) != '\n') {
if (isalpha(input)) {
letters++;
} else if (isdigit(input)) {
digits++;
} else if (isspace(input)) {
spaces++;
} else {
others++;
}
}
printf("字母个数:%d\n", letters);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他字符个数:%d\n", others);
return 0;
}
```
输入输出示例:
```
请输入一行字符:
Hello World 123!
字母个数:10
数字个数:3
空格个数:2
其他字符个数:1
```
相关问题
用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("请输入一行字符:\n");
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("输入的字符串中包含:\n");
printf("%d 个字母\n", alphabets);
printf("%d 个数字\n", digits);
printf("%d 个空格\n", spaces);
printf("%d 个其他字符\n", others);
return 0;
}
```
希望这个回答能够帮到你!
c语言输入一行字符,分别统计英文字母、空格、数字和其他字符的个数
以下是C语言统计输入字符中英文字母、空格、数字和其他字符个数的代码:
```c
#include <stdio.h>
int main()
{
char c;
int letters=0,space=0,digit=0,other=0;
printf("请输入一行字符:\n");
while((c=getchar())!='\n')
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
other++;
}
printf("字母数:%d\n空格数:%d\n数字数:%d\n其他字符:%d\n",letters,space,digit,other);
return 0;
}
```
输入一行字符后,程序会逐个字符进行判断,如果是英文字母则字母数加1,如果是空格则空格数加1,如果是数字则数字数加1,否则其他字符数加1。最后输出各个字符的个数。
阅读全文
相关推荐













