您的位置:首页 > 其它

编写一个程序,输入一行字符,以回车结束,分别统计出其中的英文字母、空格、数字和其他字符的数

2012-04-11 01:33 1306 查看
#include <stdio.h>
int main()
{
int letter=0,space=0,digit=0,others=0; //声明英文字母,空格,数字和其他字符的计数变量初始化为0
char c;  //声明接收字符串的变量
while((c=getchar())!='\n'){  // 以回车符为结束的判断标记
if(c==' ')  // 检测到空格
space++;
else if(c>='0'&&c<='9') // 检测到数字
digit++;
else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) // 检测到字母,要同时考虑字母的大小写
letter++;
else others++;
}
//输出结果
printf("The number of letters is:%d\n",letter);
printf("The number of spaces is:%d\n",space);
printf("The number of digits is:%d\n",digit);
printf("The number of other words is:%d\n",others);
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  include c
相关文章推荐