posts - 137,comments - 0,views - 40818

第一个函数的任务是判断 string 对象中是否含有大写字母,无须修改参数的内容,因此将其设为常量引用类型。第二个函数需要修改参数的内容,所以应该将其设定为非常量引用类型。满足题意的程序如下所示:

复制代码
#include <iostream>
#include <Windows.h>
using namespace std;
bool hasUpper(const string& str) { //判断字符串中是否含有大写字母
    for (auto c:str) {
        if (isupper(c)) {
            return true;
        }
    }
    return false;
}
void ChangeToLower(string& str) { //把字符串中的大写字母转换为小写字母
    for (auto& c : str) {
        c = tolower(c);
    }
}
int main() {
    string str;
    cout << "请输入一个字符串:" << endl;
    cin >> str;
    if (hasUpper(str)) {
        ChangeToLower(str);
        cout << "转换后的字符串为:" << str << endl;
    }
    else {
        cout << "字符串中无大写字母,无需转换!" << endl;
    }
}
复制代码

posted on   wshidaboss  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示