题解 | #统计字符#

统计字符

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

#include <iostream>
using namespace std;
#include <cctype>
#include <string>
int main() {
    string str;
    getline(cin,str);
    int en = 0;
    int space = 0;
    int num = 0;
    int others = 0;
    for(char i : str){
        if(isalpha(i)){
            en++;
        }
        else if(i == ' '){
            space++;
        }
        else if(isdigit(i)){
            num++;
        }
        else{
            others++;
        }
    }
    cout << en << endl;
    cout << space << endl;
    cout << num << endl;
    cout << others << endl;
}

可以用cctype库,如果不用,那用ASCII码来判断也是完全没问题的

华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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