c语言while循环编写统计字符串中英文字母、空格、数字和其他字母个数
时间: 2023-05-28 20:04:56 浏览: 173
```c
#include <stdio.h>
int main()
{
char str[100];
int i = 0, letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入字符串:");
gets(str);
while(str[i] != '\0')
{
if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
letters++;
}
else if(str[i] == ' ')
{
spaces++;
}
else if(str[i] >= '0' && str[i] <= '9')
{
digits++;
}
else
{
others++;
}
i++;
}
printf("字母个数:%d\n空格个数:%d\n数字个数:%d\n其他字符个数:%d\n", letters, spaces, digits, others);
return 0;
}
```
相关问题
统计其中英文字母、空格、数字和其他字符的个数C语言
以下是统计字符串中英文字母、空格、数字和其他字符个数的C语言代码:
```c
#include <stdio.h>
#include <ctype.h>
void count(char str[]) {
int letter = 0, space = 0, digit = 0, other = 0;
int i = 0;
while (str[i] != '\0') {
if (isalpha(str[i])) {
letter++;
} else if (isspace(str[i])) {
space++;
} else if (isdigit(str[i])) {
digit++;
} else {
other++;
}
i++;
}
printf("字母个数:%d\n空格个数:%d\n数字个数:%d\n其他字符个数:%d\n", letter, space, digit, other);
}
int main() {
char str[100];
printf("请输入字符串:");
gets(str);
count(str);
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。最后输出各个字符的个数。