键盘输入字符串,遇‘#’结束,分别统计字母、数字和其它字符个数。

2024-12-28 03:16:37
推荐回答(2个)
回答1:

#include
int main()
{
char ch;
int num,letter,other;
num=letter=other=0;
while(1)
{
scanf("%c",&ch);
if(ch=='#')break;
if(ch>='0'&&ch<='9')num++;
else
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))letter++;
else other++;
}
printf("数字个数:%d\n字母个数:%d\n其他字符个数:%d\n",num,letter,other);

return 0;
}

回答2:

#include "stdio.h"

void main()
{
char c;
int num,cha,other;
num=cha=other=0;
while((c=getchar())!='#')
{
if(c>='0' && c<='9')
num++;
else if((c>='a' && c<='z') || (c>='A' && c<='Z'))
cha++;
else other++;
}
printf("Shuzi:%d\nZimu:%d\nOther:%d\n",num,cha,other);
}