输入一行字符,要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数

最近看统计字符的题目,随后自己编写了程序,按要求统计出大写字母,小写字母,数字,空格还有其他的字符个数,其实主要是考察字符数组使用,下面是自己编写的程序:

#include <stdio.h>

int main()
{ char  a[80]; //存放字符
  char str;
  int i;
  int b[5]={0}; //存放统计的个数
  gets(a);

  for(i=0;(str=a[i])!='\0';i++)
  {
   if(str>='a'&&str<='z') //统计小写字母个数
     b[0]++;
    else  if(str>='A'&&str<='Z')//统计大写字母个数
        b[1]++;
     else if(str>='0'&&str<='9') //统计数字个数
        b[2]++;
      else if(str==' ')  //统计空格个数
           b[3]++;
        else b[4]++;  //统计其他字符个数

  }
  printf("小写字母:%d\n",b[0]);
  printf("大写字母:%d\n",b[1]);
  printf("数字:%d\n",b[2]);
  printf("空格:%d\n",b[3]);
  printf("其他字符:%d\n",b[4]);

return 0;

}
 

猜你喜欢

转载自blog.csdn.net/wanrxpjing/article/details/81160626
今日推荐