题解 | #字符串加解密#

字符串加解密

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

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

string encode(string str) {
    string res = str;
    for (int i = 0; i < str.length(); ++i) {
        if (isalpha(str[i]) && isupper(str[i])) {
            res[i] = 'a' + (((res[i] - 'A') + 1) % 26);
        } else if (isalpha(str[i]) && islower(str[i])) {
            res[i] = 'A' + (((res[i] - 'a') + 1) % 26);
        }else if(isdigit(str[i])){
            res[i] = '0' + (((res[i] - '0') + 1) %10) ;
        }else{
            res[i]= str[i];
        }
    }
    return res;
}
string decode(string str) {
    string res = str;
    for (int i = 0; i < str.length(); ++i) {
        if (isalpha(str[i]) && isupper(str[i])) {
            res[i] = 'a' + (((26+res[i] - 'A') - 1) % 26);
        } else if (isalpha(str[i]) && islower(str[i])) {
            res[i] = 'A' + (((26+res[i] - 'a') - 1) % 26);
        }else if(isdigit(str[i])){
            res[i] = '0' + (((10+res[i] - '0') - 1) %10) ;
        }else{
            res[i]=str[i];
        }
    }
    return res;
}
int main() {
    string str1, str2;
    // string ans1 = "", ans2 = "";
    cin >> str1 >> str2;
    cout<<encode(str1)<<endl;
    cout<<decode(str2)<<endl;
}
// 64 位输出请用 printf("%lld")

#23届找工作求助阵地#
全部评论

相关推荐

点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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