题解 | #字符串加解密#

字符串加解密

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

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

int main() {
    string s1, s2;
    while (cin >> s1 >> s2) {
        //对s1进行加密
        int n = s1.length();
        string jiami = "";
        for (int i; i < n; i++) {
            char temp;
            if (s1[i] - '9' <= 0) { //数字码
                temp = '0' + (s1[i] - '0' + 1) % 10;
            } else if (s1[i] - 'Z' <= 0) { //大写字母码
                temp = 'A' + (s1[i] - 'A' + 1) % 26 + 32;
            } else { //小写字母码
                temp = 'a' + (s1[i] - 'a' + 1) % 26 - 32;
            }
            jiami += temp;
        }
        cout << jiami << endl;
        //对s2进行解密
        int m = s2.length();
        string jiemi = "";
        for (int i; i < m; i++) {
            char temp;
            if (s2[i] - '9' <= 0) { //数字码
                temp = '0' + (s2[i] - '0' - 1+10) % 10;
            } else if (s2[i] - 'Z' <= 0) { //大写字母码
                temp = 'A' + (s2[i] - 'A' - 1+26) % 26 + 32;
            } else { //小写字母码
                temp = 'a' + (s2[i] - 'a' - 1+26) % 26 - 32;
            }
            jiemi += temp;
        }
        cout << jiemi << endl;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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