【华为OJ】输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数

来源:互联网 发布:彩票组合软件手机软件 编辑:程序博客网 时间:2024/05/16 23:47

描述

输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

 

    /**
     * 统计出英文字母字符的个数。
     * 
     * @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;
    }

 

 

知识点字符串运行时间限制10M内存限制128输入

输入一行字符串,可以有空格

输出

统计其中英文字符,空格字符,数字字符,其他字符的个数

样例输入1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][样例输出26 3 10 12思路:

这道题很简单,遍历判断即可


源代码:

  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4. int getEngcharNum(string s){  
  5.     int EngcharNum = 0;  
  6.     int size = s.size();  
  7.     for(int i = 0; i < size; i++)  
  8.         if((s[i] <= 'z' && s[i] >= 'a') || (s[i] <= 'Z' && s[i] >= 'A'))  
  9.             EngcharNum++;  
  10.     return EngcharNum;  
  11. }  
  12. int getBlankcharNum(string s){  
  13.     int BlankcharNum = 0;  
  14.     int size = s.size();  
  15.     for(int i = 0; i < size; i++)  
  16.         if(s[i] == ' ')  
  17.             BlankcharNum++;  
  18.     return BlankcharNum;  
  19. }  
  20. int getDigitcharNum(string s){  
  21.     int DigitcharNum = 0;  
  22.     int size = s.size();  
  23.     for(int i = 0; i < size; i++)  
  24.         if(s[i] <= '9' && s[i] >= '0')  
  25.             DigitcharNum++;  
  26.     return DigitcharNum;      
  27. }  
  28. int getOthercharNum(string s){  
  29.     int OthercharNum = 0;  
  30.     int size = s.size();  
  31.     return (size - getEngcharNum(s) - getBlankcharNum(s) - getDigitcharNum(s));  
  32. }  
  33. void main(){  
  34.     string str;  
  35.     getline(cin, str);  
  36.     cout << getEngcharNum(str) << endl << getBlankcharNum(str) << endl << getDigitcharNum(str) << endl << getOthercharNum(str)<<endl;  
  37. }  








0 0