文字描述转化为代码/C++

字符串加解密

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

/*
最简单的写法————把每种case都写出来

*/
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <algorithm>
#include <unordered_map>
using namespace std;

string encoder(string &str){
    string ans;

    for(char c: str){
        if(c == 'z')
            ans += 'A';
        else if(c == 'Z')
            ans += 'a';
        else if(c >= 'a' && c <= 'y')
            ans += c + 1 - 32;
        else if(c >= 'A' && c <= 'Y')
            ans += c + 1 + 32;
        else if(c == '9')
            ans += '0';
        else if(c >= '0' && c <= '8')
            ans += c+1;
        else
            ans += c;
    }

    return ans;
}

string decoder(string &str){
    string ans;

    for(char c: str){
        if(c == 'A')
            ans += 'z';
        else if(c == 'a')
            ans += 'Z';
        else if(c >= 'b' && c <= 'z')
            ans += c - 1 - 32;
        else if(c >= 'B' && c <= 'Z')
            ans += c - 1 + 32;
        else if(c == '0')
            ans += '9';
        else if(c >= '1' && c <= '9')
            ans += c-1;
        else
            ans += c;
    }

    return ans;
}

int main(){
    string str1, str2;
    while(cin>>str1>>str2){
        cout<<encoder(str1)<<endl;
        cout<<decoder(str2)<<endl;
    }
    return 0;
}

全部评论

相关推荐

白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
04-09 09:47
门头沟学院 Java
Arbelite_:2-3k,这工资还不如去摇奶茶
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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