优雅的C++

统计字符

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

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    while (getline(cin, str))
    {
        int a = 0, b = 0, c = 0, d = 0;
        for (const auto item : str)
        {
            if (isalpha(item))
                ++a;
            else if (isspace(item))
                ++b;
            else if (isdigit(item))
                ++c;
            else if (ispunct(item))
                ++d;
        }
        cout << a << endl << b << endl << c << endl << d << endl;
    }
    return 0;
}

全部评论
数字没统计到 循环不用迭代才行 迭代器定义了char型 所以count不到数字了
2 回复 分享
发布于 2021-09-27 18:23
for(auto i:str){...} 这么写for循环还能更优雅哈哈哈
点赞 回复 分享
发布于 2022-12-04 08:14 英国

相关推荐

06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
36
1
分享

创作者周榜

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