题解 | #统计字符#

统计字符

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);
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 17:13
想去,但是听说加班强度实在难崩,所以拒绝了,现在有点心梗对面hr感觉也是实习生,打电话的时候怪紧张的,但是感觉人很好嘞
水中水之下水道的鼠鼠:哥们这不先去体验一下,不行再跑呗,大不了混个实习经历(有更好的转正offer就当我没说)
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 14:35
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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