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

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

https://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;
}

全部评论

相关推荐

06-23 18:25
沈阳大学 Java
HR已读不回,是我说话方式不对吗?
大白之主:你是串子吗? hr: 我们不招人了,把岗位挂着boss只是因为我闲得慌
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
05-05 21:45
已编辑
广州大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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