题解 | #字符串加解密#

字符串加解密

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

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

void encoder(string& str) {
    int N = str.size();
    for (int i = 0; i < N; i++) {
        if (str[i] >= 'a' && str[i] <= 'z') {
            if (str[i] == 'z') {
                str[i] = 'A';
                continue;
            }
            str[i] = toupper(str[i] + 1);
        } else if (str[i] >= 'A' && str[i] <= 'Z') {
            if (str[i] == 'Z') {
                str[i] = 'a';
                continue;
            }
            str[i] = tolower(str[i] + 1);
        } else if (str[i] >= '0' && str[i] <= '9') {
            if (str[i] == '9') {
                str[i] = '0';
                continue;
            }
            str[i] = str[i] + 1;
        }
    }
}

void decoder(string& str) {
    int N = str.size();
    for (int i = 0; i < N; i++) {
        if (str[i] >= 'a' && str[i] <= 'z') {
            if (str[i] == 'a') {
                str[i] = 'Z';
                continue;
            }
            str[i] = toupper(str[i] - 1);
        } else if (str[i] >= 'A' && str[i] <= 'Z') {
            if (str[i] == 'A') {
                str[i] = 'z';
                continue;
            }
            str[i] = tolower(str[i] - 1);
        } else if (str[i] >= '0' && str[i] <= '9') {
            if (str[i] == '0') {
                str[i] = '9';     
                continue;           
            }
            str[i] = str[i] - 1;
        }
    }
}

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

模拟

#华为机试#
华为OD机测试题 文章被收录于专栏

个人练习专栏

全部评论

相关推荐

07-04 16:00
门头沟学院 Java
点赞 评论 收藏
分享
07-03 16:02
门头沟学院 Java
今天面试,非常紧张,面试官问我springboot有哪些核心模块都答不上来了,真的对自己无语了!
程序员小白条:28届我勒个去,很多人面试都没机会
查看1道真题和解析
点赞 评论 收藏
分享
05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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