0
点赞
收藏
分享

微信扫一扫

华为机试题——HJ40 统计字符

前行的跋涉者 2022-04-21 阅读 46
c++

描述

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

数据范围:输入的字符串长度满足 1 \le n \le 1000 \1≤n≤1000 

输入描述:

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

输出描述:

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

示例1

输入:

1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][

复制输出:

26
3
10
12
#include <iostream>
#include <string.h>
#include <algorithm>
#include <string.h>
#include <sstream>

void StatisticsChar(std::string str)
{
    int len = strlen(str.c_str());
    int englishChar = 0;            //英文字符个数
    int spaceChar = 0;              //空格个数
    int numberChar = 0;             //数字个数
    int othreChar = 0;              //其他
    for(int i = 0; i < len; ++i)
    {
        char c = str[i];
        if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
        {
            englishChar++;
        }
        else if(c == ' ')
        {
            spaceChar++;
        }
        else if(c >= '0' && c <= '9')
        {
            numberChar++;
        }
        else
        {
            othreChar++;
        }
    }
    std::cout << englishChar << std::endl;
    std::cout << spaceChar << std::endl;
    std::cout << numberChar << std::endl;
    std::cout << othreChar << std::endl;
}

int main()
{
    std::string str;
    getline(std::cin, str);
    StatisticsChar(str);

    

    return 0;
} 
举报

相关推荐

华为机试HJ40统计字符

华为机试题——HJ94 记票统计

华为机试题102-字符统计

华为机试题——HJ12 字符串反转

华为机试题——HJ13 句子逆序

华为机试HJ4 字符串分隔

0 条评论