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

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

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

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string str;
    getline(cin, str);

    int alphabets = 0;
    int digits = 0;
    int whitespace = 0;
    int others = 0;

    // panduan zifuchuan
    for (int i = 0; i < str.length(); ++i)
    {
        if (isalpha(str[i]))
        {
            ++alphabets;
        }
        else if (isdigit(str[i]))
        {
            ++digits;
        }
        else if (isspace(str[i]))
        {
            ++whitespace;
        }
        else
        {
            ++others;
        }
    }

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


    return 0;
}

这个就是要知道string类型的一些用法, 并且string 类型可以像一般的字符串一样按下标访问
全部评论

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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