//输入一行字符,统计出其中英文字母,空格,数字和其他字符的数量
#include<stdio.h>
int main()
{
char c;
int letter=0, space=0,digit=0,other=0;
while(c=getchar()!='\n')
{
if(c>='A'&&c<='Z'||c>='a'&&c<='z')
letter++;
else if(c==' ')
space++;
else if(c>=0&&c<=9)
digit++;
else
other++;
}
printf("letter=%d,space=%d,digit=%d,other=%d",letter,space,digit,other);
return 0;
}
,结果不对,实在找不出问题了
修改处见注释,供参考:
#include<stdio.h>
int main()
{
char c;
int letter=0, space=0,digit=0,other=0;
while((c=getchar())!='\n') //while(c=getchar()!='\n') 修改
{
if(c>='A'&&c<='Z'||c>='a'&&c<='z')
letter++;
else if(c==' ')
space++;
else if(c>='0' && c<='9') //if(c>=0&&c<=9) 修改
digit++;
else
other++;
}
printf("letter=%d,space=%d,digit=%d,other=%d",letter,space,digit,other);
return 0;
}
第二个else if里的0和9没有加单引号,判断的并不是数字0~9,而是ASCII里第0个字符和第9个字符之间的字符
这篇文章:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数 也许能够解决你的问题,你可以看下