题解 | #字符串加解密#

字符串加解密

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机测试题 文章被收录于专栏

个人练习专栏

全部评论

相关推荐

服从性笔试吗,发这么多笔,现在还在发。
蟑螂恶霸zZ:傻 x 公司,发两次笔试,两次部门匹配挂,
投递金山WPS等公司10个岗位 >
点赞 评论 收藏
分享
把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务