C语言 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

要求:

    1)用while循环及getchar()函数完成字符串输入,回车键结束。

    2)分别统计英文字母、空格、数字和其它字符的个数。

    3)打印统计结果。

运行参考示例:

输入1:long int sn=10,tn=0;

输出1:char=11 space=2 digit=3 others=4


#include
int main()
{
 char c;
 int ch,s,d,o;
 ch=0;
 s=0;
 d=0;
 o=0;
 
 while((c=getchar())!='\n')
 {
     if(c>='a'&&c<='z'||c>='A'&&c<='Z')
        ch++;
     else if(c==' ')
        s++;
     else if(c>='0'&&c<='9')
        d++;
     else 
        o++;
  
 }
 printf("char=%d space=%d digit=%d others=%d\n",ch,s,d,o);
 return 0;
}

你可能感兴趣的:(c语言)