题解 | #统计大写字母个数#

统计大写字母个数

http://www.nowcoder.com/practice/434414efe5ea48e5b06ebf2b35434a9c

题意:
        找出给定字符串中大写字符(即'A'-'Z')的个数。

方法一:
循环模拟

思路:
        一重循环,累加大写字符个数。
        

#include <bits/stdc++.h>

using namespace std;

int main(){
    string s;
    while(getline(cin,s)){//输入
        int len=s.size();
        int res=0;
        for(int i=0;i<len;i++){
            if(s[i]>='A'&&s[i]<='Z')//累加大写字符个数
                res++;
        }
        cout << res << endl;
    }
    return 0;
}

时间复杂度:
空间复杂度:

方法二:
C++函数

思路:
        isupper()函数判断是否为大写字母,循环遍历并通过大写字母的个数。

#include <bits/stdc++.h>

using namespace std;
int cnt[26];

int main(){
    string s;
    while(getline(cin,s)){//输入
        int len=s.size();
        int res=0;
        for(int i=0;i<len;i++){
            if(isupper(s[i]))//累加大写字符个数
                res++;
        }
        cout << res << endl;
    }
    return 0;
}

时间复杂度:
空间复杂度:



全部评论

相关推荐

2025-11-19 18:44
已编辑
成都理工大学 Java
程序员花海:我面试过100+校招生,大厂后端面试不看ACM,竞赛经历含金量低于你有几份大厂实习 这个简历整体来看不错 可以海投
如何写一份好简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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