题解 | #统计字符#

统计字符

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

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

int main() {
    string str;
    getline(cin,str);
    int word = 0, space = 0, number = 0, other = 0;
    for(int i = 0; i < str.size(); ++i){
        if(str[i]>='a'&&str[i]<='z' || str[i]>='A'&&str[i]<='Z')    word++;
        else if(str[i] == ' ')                                      space++;
        else if(str[i]>='0'&&str[i]<='9')                           number++;
        else                                                        other++;                                     
    }
    cout << word << endl << space << endl << number << endl << other << endl;
    return 0;
}

全部评论

相关推荐

吴offer选手:学到了,下次面试也放张纸在电脑上,不然老是忘记要说哪几个点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务