华为机试:字符串加解密

#include <iostream>
#include <string>
using namespace std;

string en_or_decryption (string str, int oper) {
    for (auto c = 0; c < str.length(); c++) {
        if (str[c] >= '0' && str[c] <= '9') {
            if (oper) {
                str[c] = (str[c] + 1) > '9' ? '0' : (str[c] + 1);
            }
            else {
                str[c] = (str[c] - 1) < '0' ? '9' : (str[c] - 1);
            }
        }
        else if (str[c] >= 'A' && str[c] <= 'Z') {
            if (oper) {
                str[c] = tolower(str[c]);
                str[c] = (str[c] + 1) > 'z' ? 'a' : (str[c] + 1);
            }
            else {
                str[c] = tolower(str[c]);
                str[c] = (str[c] - 1) < 'a' ? 'z' : (str[c] - 1);
            }
        }
        else {
            if (oper) {
                str[c] = toupper(str[c]);
                str[c] = (str[c] + 1) > 'Z' ? 'A' : (str[c] + 1);
            }
            else {
                str[c] = toupper(str[c]);
                str[c] = (str[c] - 1) < 'A' ? 'Z' : (str[c] - 1);
            }
        }
    }
    return str;
}

int main() {
    // int a, b;
    // while (cin >> a >> b) { // 注意 while 处理多个 case
    //     cout << a + b << endl;
    // }
    string encpt;
    cin >> encpt;
    string decpt;
    cin >> decpt;
    encpt = en_or_decryption(encpt, 1);
    cout << encpt << endl;
    decpt = en_or_decryption(decpt, 0);
    cout << decpt << endl;
    return 0;
}

全部评论

相关推荐

重生我想学测开:嵌入式的问题,我准备入行京东外卖了
点赞 评论 收藏
分享
04-25 19:29
已编辑
宁波大学 运营
被普调的六边形战士很高大:你我美牛孩
点赞 评论 收藏
分享
评论
1
7
分享

创作者周榜

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