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

#include <stdio.h>
void main ()

{char x;
printf ("请输入一串字符\n");
scanf("%c",&x);
int letter=0,blank=0,number=0,other=0;
while((x=getchar( ))!='\n')
{
if('a'<=x&&x<='z'||'A'<=x&&x<='Z')
letter++;
else if(x==' ')
blank++;
else if('0'<=x&&x<='9')
number++;
else
other++;
}

printf("字母的个数%d,空格的个数%d,数字的个数%d,其他符号的个数%d",letter,blank,number,other);

求解,错在哪里了呢,为什么输入的第一个符号不能读出来

#include<stdio.h>

int main()

{

char c;

int letters=0,spaces=0,digits=0,others=0;

printf("请输入一串任意的字符:\n");

while((c=getchar())!='\n')

{

if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))

letters++;

else if(c>='0'&&c<='9')

digits++;

else if(c==' ')

spaces++;

else

others++;

}

printf("字母有%d个,数字有%d个,空格有%d个,其他有%d个",letters,digits,spaces,others);

return 0;

}

扩展资料:

while语句若一直满足条件,则会不断的重复下去。但有时,需要停止循环,则可以用下面的三种方式:

一、在while语句中设定条件语句,条件不满足,则循环自动停止。

如:只输出3的倍数的循环;可以设置范围为:0到20。

二、在循环结构中加入流程控制语句,可以使用户退出循环。

1、break流程控制:强制中断该运行区内的语句,跳出该运行区,继续运行区域外的语句。

2、continue流程控制:也是中断循环内的运行操作,并且从头开始运行。

三、利用标识来控制while语句的结束时间。

参考资料来源:

百度百科——while

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-03-24
第2个回答  2020-03-09

C语言经典例子之统计英文、字母、空格及数字个数

第3个回答  2012-11-30

#include <stdio.h>

 void main ()

{char x;

    printf ("请输入一串字符\n");

    //scanf("%c",&x);     这一行应该去掉

    int letter=0,blank=0,number=0,other=0;

    while((x=getchar( ))!='\n')

    {

        if('a'<=x&&x<='z'||'A'<=x&&x<='Z')   

        letter++;

    else if(x==' ')

        blank++;

else if('0'<=x&&x<='9')     

   number++;

    else 

        other++;

    }

    printf("字母的个数%d,空格的个数%d,数字的个数%d,其他符号的个数%d",letter,blank,number,other);

}





这样应该可以了,你试试看。

来自:求助得到的回答
第3个回答  2012-11-30
这个问题,在《C语言程序设计》中有提到,在P78,下面附上代码,具体情况你自己看吧:
//输入一行字符,统计并输出其中的英文字符,数字字符,空格和其他字符的个数
#include""
void main()
{
char ch;
//letter英文字符,space数字字符,digit空格,other其他字符
int letter=0,space=0,digit=0.other=0;
printf("请输入一行字符:\n");
while((ch=getchar())!='\n') //设置循环条件为输入的字符不是回车符
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
letter++;
else if(ch==' ')
space++;
else if(ch>='0'&&ch<='9')
digit++;
else
other++;
printf("字符%d个,空格%d个,数字%d个,其它字符%d个",letter,space,digit,other);
}本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网