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

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

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

#include <cctype>
#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......
    int i = 0;
    while (str[i]!='\0') {
        if (isalpha(str[i])) {
            ++chars;
        }else if(isdigit(str[i])) {
            ++digits;
        }else if(isspace(str[i])){
            ++whitespace;
        }else {
        ++others;
        }
        i++;
    
    }

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

    return 0;
}

全部评论

相关推荐

03-03 23:12
已编辑
北京邮电大学 Java
后测速成辅导一两个月...:写挺好的,建议尽早投简历,抢在hc多的时候开始面试
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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