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

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

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

可以使用C++ string 内置函数实现

#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++;
            continue;
        }
        if (isdigit(str[i])){
            digits++;
            continue;
        }
        if (isspace(str[i])){
            whitespace++;
            continue;
        }
        else{
            others++;
            continue;
        }
    }

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

    return 0;
}
全部评论

相关推荐

03-03 19:02
已编辑
东华理工大学 Node.js
点赞 评论 收藏
分享
愤怒的查理被升职:每个项目经历后面加关键成果:xxx, 项目经历要和岗位JD匹配,不是有多少项目经历都要写上的,挑选出是个岗位JD的,并且内容往岗位JD方面写,教育背景直接写 大学+专业+时间,一行就过去了。你是建筑投运营别人不看你本科建筑学的怎么样
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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