从键盘输入一个字符串存入字符数组,统计数字,字母,空格和其他字符的个数.

用gets、 puts 解答,尽量简单点。

#include<stdio.h>

int main()

{

  char str[100];

  int i=0;

  int num=0,ch=0,blank=0,other=0;

 

  gets(str);

  while(str[i]!='\0')

  {

    if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))

      ch++;//字母

    else if(str[i]>='0' && str[i]<='9')

      num++;//数字

    else if(str[i]==' ')

      blank++;//空格

    else

      other++;

 

    i++; 

   }

  printf("数字%d个,字母%d个,空格%d个,其他%d个\n",num,ch,blank,other);

  return 0;

}

 

望采纳,谢谢!!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-24
如果是计算总共的字符:用字符串函数strlen();
如果是数字,字母,其它的个数分别都算:你用个if语句分别用ASCII码卡定数字,字母的范围,用一个计数器count++。
相似回答