c语言统计文章中的字母,数字,空格个数

2024-11-06 07:28:58
推荐回答(1个)
回答1:

#include
int main()
{
int letter=0,space=0,number=0,others=0;
char nextchar;
printf("Input your string\n");
for(;nextchar!='\n';)
{
scanf("%c",&nextchar);
if(('a'<=nextchar&&nextchar<='z')||('A'<=nextchar&&nextchar<='Z'))
letter++;
else if(nextchar==' ')
space++;
else if('0'<=nextchar&&nextchar<='9')
number++;
else
others++;
}
printf("letter=%d,space=%d,number=%d,others=%d\n",letter,space,number,--others);
return 0;
}