#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语句的结束时间。
参考资料来源:
C语言经典例子之统计英文、字母、空格及数字个数
#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);
}
这样应该可以了,你试试看。
来自:求助得到的回答