標(biāo)題: c++統(tǒng)計(jì)字符個(gè)數(shù)程序 [打印本頁(yè)]

作者: daming    時(shí)間: 2014-12-30 02:07
標(biāo)題: c++統(tǒng)計(jì)字符個(gè)數(shù)程序
  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])                                                        //以回車(chē)結(jié)束,‘\0’的ASKII碼是“0”
  11. {
  12.        if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
  13.               a++;                                                     //字母?jìng)(gè)數(shù)
  14.        else
  15.              if(str[i]>='0'&&str[i]<='9')
  16.                     b++;                                                 //數(shù)字個(gè)數(shù)  
  17.              else
  18.                     if(str[i]==' ')
  19.                             c++;                                          //空格個(gè)數(shù)
  20.                     else
  21.        d++;                                                               //其他
  22.         i++;                     
  23. }
  24. cout<<"字母="<<a<<endl                                  // 輸出
  25.         <<"數(shù)字="<<b<<endl
  26.         <<"空格="<<c<<endl
  27.         <<"其他="<<d<<endl;
  28. }
復(fù)制代碼







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1