输入一行字符,分别统计出其中的大小写英文字母、空格、数字和其它字符的个数。
时间: 2023-11-25 18:50:39 浏览: 82
以下是C语言实现输入一行字符,分别统计出其中的大小写英文字母、空格、数字和其它字符的个数的代码:
```c
#include <stdio.h>
int main()
{
char c;
int y=0,s=0,k=0,z=0;
printf("请输入一串字符:");
while((c=getchar())!='\n')
{
if(c<='z'&&c>='a'||c<='Z'&&c>='z') y++;
else if(c<='9'&&c>='0') s++;
else if(c==' ') k++;
else z++;
}
printf("英文字母有%d个,数字有%d个,空格有%d个,其他字符有%d个",y,s,k,z);
return 0;
}
```
相关问题
c语言输入一行字符,分别统计出其中的大小写英文字母、空格、数字和其他字符的个数
这是一个C语言程序,可以输入一行字符并统计其中的大小写英文字母、空格、数字和其他字符的个数。具体实现方法如下:
```
#include <stdio.h>
int main()
{
char c;
int letters = 0, space = 0, digit = 0, other = 0;
while ((c = getchar()) != '\n')
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
letters++;
else if (c >= '0' && c <= '9')
digit++;
else if (c == ' ')
space++;
else
other++;
}
printf("字母数:%d\n空格数:%d\n数字数:%d\n其他字符:%d\n", letters, space, digit, other);
return 0;
}
```
程序中使用了一个while循环,每次读入一个字符并进行判断,如果是大小写英文字母,则字母数加1;如果是数字,则数字数加1;如果是空格,则空格数加1;否则其他字符数加1。最后使用printf函数输出统计结果。
2. 输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。
这个问题需要编写一个程序来实现。程序的基本思路是使用while循环读取输入的字符,然后根据字符的类型进行计数。具体实现可以参考上面提供的三个引用中的任意一个程序。需要注意的是,英文字母包括大小写,空格是一个字符,数字是0-9的数字字符。其他字符则是除了上述三种类型以外的所有字符。下面是一个示例程序:
```
#include <stdio.h>
int main() {
char c;
int letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入一行字符:");
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个,数字有%d个,空格有%d个,其他字符有%d个\n", letters, digits, spaces, others);
return 0;
}
```