题解 | #字符串加解密#

字符串加解密

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

思路:根据题目编写

#include <iostream>
using namespace std;

void encoder(string& str) {
    int cha = 'a' - 'A';
    for (int i = 0; i < str.size(); i++) {
        char c = str[i];
        if ('a' <= c && c <= 'z') {
            if (c == 'z') {
                c = 'a';
            } else {
                c = c + 1;
            }
            c = c - cha;
        } else if ('A' <= c && c <= 'Z') {
            if (c == 'Z') {
                c = 'A';
            } else {
                c = c + 1;
            }
            c = c + cha;
        } else {
            int a = c - '0';
            a = (a + 1) % 10;
            c = a + '0';
        }
        str[i] = c;
    }
}

void decoder(string& str) {
    int cha = 'a' - 'A';
    for (int i = 0; i < str.size(); i++) {
        char c = str[i];
        if ('a' <= c && c <= 'z') {
            if (c == 'a') {
                c = 'z';
            } else {
                c = c - 1;
            }
            c = c - cha;
        } else if ('A' <= c && c <= 'Z') {
            if (c == 'A') {
                c = 'Z';
            } else {
                c = c - 1;
            }
            c = c + cha;
        } else {
            int a = c - '0';
            if(a == 0) {
                a = 9;
            }
            else {
                a = a - 1;
            }
            c = a + '0';
        }
        str[i] = c;
    }
}

int main() {
    string str1, str2;
    cin >> str1 >> str2;
    encoder(str1);
    decoder(str2);
    cout << str1 << endl;
    cout << str2 << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-08 23:37
已编辑
东华大学 结构工程师
点赞 评论 收藏
分享
HoePointer:把重点可以标黑,简历精简一下,然后把你的项目放在 github 或者 gitee 上面,readme 写好看一点(一般面试官有可能会翻你的网页)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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