题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
int char_num = 0;
int space_num = 0;
int digit_num = 0;
int  other_num = 0;
bool isspace(char ch) {
    return ch == ' ';
}
void count_function(const string& str) {
    for (auto it = str.begin(); it != str.end(); it++) {
        if (isalpha(*it)) { // 判断是否为字母
            char_num ++;

        } else if (isspace(*it)) { // 判断是否为空格
            space_num++;
        } else if (isdigit(*it)) { // 判断是否为数字
            digit_num ++;
        } else { //为其他字符
            other_num++;
        }
    }
    cout <<  char_num  << endl;
    cout <<  space_num  << endl;
    cout <<  digit_num  << endl;
    cout <<  other_num  << endl;
}
int main(void) {
    string str;
    getline(cin, str);
    count_function(str);
}

全部评论

相关推荐

牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
07-14 12:29
门头沟学院 Java
后端岗,实习三周感觉有点想跑路了,担心秋招被拉黑,有没有佬是字节HR知道情况的
从零开始的转码生活:你实习三周都想跑路,将来拿到offer真的愿意在这干十几二十年吗
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务