编程:输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。

如题所述

#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个回答  推荐于2017-12-16
clear
accept "请输入一串字符:" to x
store 0 to dyw,xyw,kg,sz,qt
m=len(x)
for i=1 to m
x1=substr(x,i,1)
k=asc(x1)
do case
case k=32
kg=kg+1
case k>=48 and k<=57
sz=sz+1
case k>=65 and k<=90
dyw=dyw+1
case k>=97 and k<=122
xyw=xyw+1
other
qt=qt+1
endcase
endfor
?"其中空格有: "+alltrim(str(kg))+"个"
?"大写字母有: "+alltrim(str(dyw))+"个"
?"小写字母有: "+alltrim(str(xyw))+"个"
?"数字有: "+alltrim(str(sz))+"个"
?"其它字符有: "+alltrim(str(qt))+"个"本回答被网友采纳
第4个回答  推荐于2018-02-28
int main()
{
int charcount = 0, chinesecount = 0, numcount = 0, spacecount = 0, othercount = 0, totalcount;
cout<<"请输入一行字符:";
char pstr[] = "1 a ,,,我 朥你", curchar;
// cin>>pstr;
curchar = pstr[0];
int curindex = 1;
while(curchar != '\0')
{
if (curchar >= 65 && curchar <= 122) //A-z
charcount++;
else if (curchar >= 128 || curchar < 0) //中文
chinesecount++, curindex++; //中文字符占两个字节,因此将游标向后移一位
else if (curchar >= 48 && curchar <= 57) //0-9
numcount++;
else if (curchar == 32) //空格
spacecount++;
else
othercount++;
curchar = pstr[curindex++];
}

totalcount = charcount + chinesecount + numcount + spacecount + othercount;
cout<<"总数:"<<totalcount<<" 英文字符:"<<charcount<<" 中文字符"<<chinesecount
<<" 数字:"<<numcount<<" 空格:"<<spacecount<<" 其它:"<<othercount;
}本回答被提问者采纳
相似回答