統計各類字符個數——來自華爲OJ平臺測試基礎篇



/*
 * 描述:  輸入一行字符,分別統計出包含英文字母、空格、數字和其它字符的個數。
    **
     * 統計出英文字母字符的個數。
     * @param str 需要輸入的字符串
     * @return 英文字母的個數
     *
    public static int getEnglishCharCount(String str)
    {
        return 0;
    }
    **
     * 統計出空格字符的個數。
     * @param str 需要輸入的字符串
     * @return 空格的個數
     *
    public static int getBlankCharCount(String str)
    {
        return 0;
    }
    **
     * 統計出數字字符的個數。
     * @param str 需要輸入的字符串
     * @return 英文字母的個數
     *
    public static int getNumberCharCount(String str)
    {
        return 0;
    } 
     **
     * 統計出其它字符的個數。
     *
     * @param str 需要輸入的字符串
     * @return 英文字母的個數
     *
    public static int getOtherCharCount(String str)
    {
        return 0;
    }
 * 知識點:  字符串 
 * 題目來源:  內部整理 
 * 練習階段:  初級 
 * 運行時間限制: 10Sec
 * 內存限制: 128MByte
 * 輸入: 
 * 輸入一行字符串,可以有空格
 * 輸出: 
 * 統計其中英文字符,空格字符,數字字符,其他字符的個數
 * 樣例輸入: 1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][                   
 * 樣例輸出: 26
 * 3
 * 10
 * 12
*/

#include <iostream>
#include <string>

using namespace std;

class StatisticsNumChar
{
public:
 int EnglishCharCount;
 int BlankCharCount;
 int NumberCharCount;
 int OtherCharCount;
 std::string str;
 int size;
 
public:
 StatisticsNumChar()
 {
  EnglishCharCount = 0;
  BlankCharCount = 0;
  NumberCharCount = 0;
  OtherCharCount = 0;
  size = 0;
 }

public:
 /**
     * 統計出英文字母字符的個數。
     *
     * @param str 需要輸入的字符串
     * @return 英文字母的個數
     */
 int getEnglishCharCount()
    {
  char c;

  for(int i=0;i<size;i++)
  {
   c = str.at(i);

   if((c>=65 && c<=90) || (c>=97 && c<=122))
   {
    EnglishCharCount++;
   }
  }

        return 0;
    }
   
public:
 
    /**
     * 統計出空格字符的個數。
     *
     * @param str 需要輸入的字符串
     * @return 空格的個數
     */
 int getBlankCharCount()
    {
  char c;

  for(int i=0;i<size;i++)
  {
   c = str.at(i);

   if(c==32)
   {
    BlankCharCount++;
   }
  }

        return 0;
    }
   

public:
  /**
     * 統計出數字字符的個數。
     *
     * @param str 需要輸入的字符串
     * @return 英文字母的個數
     */
 int getNumberCharCount()
    {
  char c;

  for(int i=0;i<size;i++)
  {
   c = str.at(i);

   if((c>=48 && c<=57))
   {
    NumberCharCount++;
   }
  }

        return 0;
    }
   

public:
 /**
     * 統計出其它字符的個數。
     *
     * @param str 需要輸入的字符串
     * @return 英文字母的個數
     */
 int getOtherCharCount()
    {

  OtherCharCount = size - EnglishCharCount - BlankCharCount - NumberCharCount;

        return 0;
    }
 
public:

 void Input()
 {
  getline(std::cin,str,'\n');

  size = str.size();

  return;
 }

 void Output()
 {
  std::cout << EnglishCharCount << endl;
  std::cout << BlankCharCount << endl;
  std::cout << NumberCharCount << endl;
  std::cout << OtherCharCount << endl;

  return;
 }
};

int main()
{
 StatisticsNumChar* p = new StatisticsNumChar();

 p->Input();
 p->getEnglishCharCount();
 p->getBlankCharCount();
 p->getNumberCharCount();
 p->getOtherCharCount();
 p->Output();

 delete p;

 system("pause");

 return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章