输入一段字符,分别统计出其中英文字母、空格、数字和其他字符的个数,使用getchar()
时间: 2024-03-22 20:13:39 浏览: 15
函数获取用户输入。
好的,您的问题已经记录下来了。以下是您想要的代码:
```
#include <stdio.h>
int main() {
int letters = 0, spaces = 0, digits = 0, others = 0;
char c;
printf("请在此输入一段字符,按下回车键后统计其字母、空格、数字和其他字符数量:\n");
while ((c = getchar()) != '\n') {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
letters++;
} else if (c == ' ') {
spaces++;
} else if (c >= '0' && c <= '9') {
digits++;
} else {
others++;
}
}
printf("英文字母数量:%d\n空格数量:%d\n数字数量:%d\n其他字符数量:%d\n", letters, spaces, digits, others);
return 0;
}
```
运行程序后,您可以输入任意字符、数字、标点符号等,程序会自动统计其中英文字母、空格、数字和其他字符的个数,然后输出结果。希望这个程序能够满足您的需求!
相关问题
用c语言的循环语句输入一行字符分别统计出其中英文字母空格数字和其他字符的个数
使用C语言的循环语句可以输入一行字符并统计其中英文字母、空格、数字和其他字符的个数。下面是一个示例代码:
```c
#include <stdio.h>
int main() {
char c;
int ch, s, d, o;
ch = 0;
s = 0;
d = 0;
o = 0;
while ((c = getchar()) != '\n') {
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
ch++;
else if (c == ' ')
s++;
else if (c >= '0' && c <= '9')
d++;
else
o++;
}
printf("char=%d space=%d digit=%d others=%d\n", ch, s, d, o);
return 0;
}
```
该代码使用了一个while循环来读取输入的字符,然后使用if语句来判断字符的类型并相应地增加计数器的值。最后,使用printf函数输出统计结果。
c语言输入一行字符分别统计其中英文字母 空格 数字 和其他字符的个数
好的,我知道了。以下是 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
```