题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

#include <iostream>
using namespace std;

void encrypt(string& str) {
    int len = str.length();
    for (int i = 0; i < len; i++) {
        // 大写字母
        if (str[i] >= 'A' && str[i] <= 'Z') {
            if (str[i] == 'Z') {
                str[i] = 'a';
            } else {
                str[i] = tolower(str[i]) + 1;
            }
        } else if (str[i] >= 'a' && str[i] <= 'z') {
            if (str[i] == 'z') {
                str[i] = 'A';
            } else {
                str[i] = toupper(str[i]) + 1;
            }
        } else {
            if (str[i] == '9') {
                str[i] = '0';
            } else {
                str[i] = (str[i]) + 1;
            }
        }
    }
    return;
}

void decrypt(string& str) {
    int len = str.length();
    for (int i = 0; i < len; i++) {
        // 大写字母
        if (str[i] >= 'A' && str[i] <= 'Z') {
            if (str[i] == 'A') {
                str[i] = 'z';
            } else {
                str[i] = tolower(str[i]) - 1;
            }
        } else if (str[i] >= 'a' && str[i] <= 'z') {
            if (str[i] == 'a') {
                str[i] = 'Z';
            } else {
                str[i] = toupper(str[i]) - 1;
            }
        } else {
            if (str[i] == '0') {
                str[i] = '9';
            } else {
                str[i] = (str[i]) - 1;
            }
        }
    }
    return;
}

int main() {
    string encryptStr;
    string decryptStr;
    cin >> encryptStr;
    cin >> decryptStr;

    encrypt(encryptStr);
    decrypt(decryptStr);

    cout << encryptStr << endl;
    cout << decryptStr << endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

09-26 19:45
门头沟学院 Java
点赞 评论 收藏
分享
08-28 20:36
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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