题解 | #字符串加解密#

字符串加解密

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

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

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

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

int main(int argc, char* argv[]){
    string unencrypted;
    getline(cin, unencrypted);
    string encrypted;
    getline(cin, encrypted);

    encrypting(unencrypted);
    unencrypting(encrypted);

    cout << unencrypted << endl;
    cout << encrypted << endl;

    return 0;
}
全部评论

相关推荐

10-28 11:04
已编辑
美团_后端实习生(实习员工)
一个2人:我说几个点吧,你的实习经历写的让人觉得毫无含金量,你没有挖掘你需求里的 亮点, 让人觉得你不仅打杂还摆烂。然后你的简历太长了🤣你这个实习经历看完,估计没几个人愿意接着看下去, sdk, 索引这种东西单拎出来说太顶真了兄弟,好好优化下简历吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务