【深基6.例1】自动修正 · 教师题解

【深基6.例1】自动修正

解题思路

遍历字符串,遇到 a~z 就换成对应大写,其他字符保持不变。

正确性说明

逐字符进行题目规定的替换,处理后每个小写字母均变为大写且其他字符不变。

易错点

数字与标点不转换;字符大小写差只用于小写范围。

复杂度

时间 O(L),空间 O(L)。

参考代码(C++11)

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin >> s;
    for (char &c : s)
        if (c >= 'a' && c <= 'z')
            c = char(c - 'a' + 'A');
    cout << s << "\n";
    return 0;
}
返回题目