题解 | 统计字符

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5?tpId=37&tqId=21263&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

#include <iostream>
using namespace std;
void to_upper(string &str){
    for(int i=0;i<str.size();i++){str[i]=toupper(str[i]);}
}
int main() {
    string s;
    int n1=0;
    int n2=0;
    int n3=0;
    int n4=0;
    to_upper(s);
    getline(cin,s);
    for(char c:s){
        if(c>='a'&&c<='z'){n1++;}
    }
    for(char c:s){
        if(c==' '){n2++;}
    }
    for(char c:s){
        if(c>='0'&&c<='9'){n3++;}
    }
    for(char c:s){
        if(c>=33&&c<=47||c>=58&&c<=64||c>=91&&c<=96||c>=123&&c<=126){n4++;}
    }
    cout<<n1<<endl;
    cout<<n2<<endl;
    cout<<n3<<endl;
    cout<<n4<<endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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