现在的位置: 首页 > 综合 > 正文

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

2013年01月18日 ⁄ 综合 ⁄ 共 484字 ⁄ 字号 评论关闭
 #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;
}


 

抱歉!评论已关闭.