top1编程
← 返回题目
题解

【入门】字符串加密

1 条题解

  • 0
    @ 2026-7-29 0:15:04
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string input;
        // 读取输入字符串
        getline(cin, input);
    
        // 遍历字符串,进行加密处理
        for (char &c : input) {
            if (c >= 'a' && c <= 'y') {
                // 小写字母 a 到 y,替换为后继字母
                c += 1;
            } else if (c == 'z') {
                // 小写字母 z,替换为 a
                c = 'a';
            } else if (c >= 'A' && c <= 'Y') {
                // 大写字母 A 到 Y,替换为后继字母
                c += 1;
            } else if (c == 'Z') {
                // 大写字母 Z,替换为 A
                c = 'A';
            }
            // 其他非字母字符保持不变
        }
    
        // 输出加密后的字符串
        cout << input << endl;
    
        return 0;
    }
    
    • 1