标题: c++统计字符个数程序 [打印本页]

作者: daming    时间: 2014-12-30 02:07
标题: c++统计字符个数程序
  1. #include"iostream"
  2. using namespace std;
  3. void main()
  4. {
  5.       char str[100];
  6.       int a,b,c,d;
  7.       a=b=c=d=0;
  8.       int i=0;
  9.       gets(str);                                                       //键入字符串
  10. while(str[i])                                                        //以回车结束,‘\0’的ASKII码是“0”
  11. {
  12.        if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
  13.               a++;                                                     //字母个数
  14.        else
  15.              if(str[i]>='0'&&str[i]<='9')
  16.                     b++;                                                 //数字个数  
  17.              else
  18.                     if(str[i]==' ')
  19.                             c++;                                          //空格个数
  20.                     else
  21.        d++;                                                               //其他
  22.         i++;                     
  23. }
  24. cout<<"字母="<<a<<endl                                  // 输出
  25.         <<"数字="<<b<<endl
  26.         <<"空格="<<c<<endl
  27.         <<"其他="<<d<<endl;
  28. }
复制代码







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1