题解 | #使用字符函数统计字符串中各类型字符的个数#

使用字符函数统计字符串中各类型字符的个数

http://www.nowcoder.com/practice/31bdbc70188f48e995fa3cbef36613c8

#include <iostream>
#include <string>


using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    for(int i=0;i<str.length();i++){
        if(isalpha(str[i])){
            chars++;
        }
        else if(isdigit(str[i])){
            digits++;
        }
        else if(isspace(str[i])){
            whitespace++;
        }
        else{
            others++;
        }
    }

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;

    return 0;
}
全部评论

相关推荐

05-16 11:16
已编辑
东华理工大学 Java
牛客737698141号:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
点赞 评论 收藏
分享
运营你豪哥:简历改改吧-非本、求职意向技术岗、无实习经历、内容空洞 如果简历不爆改的话,应该是会持续崩溃了 1.把你教育经历放最下面去 2.蓝底照片很奇怪哈,感觉还在高中时代,建议白底重新拍一下 3.校园经历没啥必要,收集和反馈同学们对产品的意见,解决学生和老师之间的沟通,企业招聘不看这些哈 好好思考一下简历的设计和你要表达的重点,再去投简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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