4.输入一行字符以@作结束标志,分别统计其中英文字母、空格、数字和其它字符的个数。C语言
时间: 2023-11-25 08:50:24 浏览: 62
以下是C语言的代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
int letter = 0, space = 0, digit = 0, other = 0;
printf("请输入一行字符,以@作为结束标志:\n");
while ((c = getchar()) != '@') {
if (isalpha(c)) {
letter++;
} else if (isspace(c)) {
space++;
} else if (isdigit(c)) {
digit++;
} else {
other++;
}
}
printf("输入的字符中,英文字母有%d个,空格有%d个,数字有%d个,其它字符有%d个。\n", letter, space, digit, other);
return 0;
}
```
相关问题
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
```
用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函数输出统计结果。