题解 | #字符串加解密#

字符串加解密

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

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

void encode(string& s){
    for(auto ch:s){
        if(isalpha(ch)){
            if((ch-'a')>=0){
                ch = toupper(ch);
                if(ch=='Z')ch='A';
                else ch++;
            }
            else{
                ch = tolower(ch);
                if(ch=='z')ch='a';
                else ch++;
            }
            cout<<ch;
        }else{
            int t = ch-'0';
            t++;
            cout<<t%10;
        }

    }
    cout<<endl;
}



void decode(string& s){
    for(auto ch:s){
        if(isalpha(ch)){
            if((ch-'a')>=0){
                if(ch=='a')ch='z';
                else ch--;
                ch = toupper(ch);
            }
            else{
                if(ch=='A')ch='Z';
                else ch--;
                ch = tolower(ch);
            }
            cout<<ch;
        }else{
            int t = ch-'0';
            t--;
            cout<<(t+10)%10;
        }

    }
    cout<<endl;
}
int main() {
    char ch;
    string s1;
    string s2;
    cin>>s1>>s2;
    encode(s1);
    decode(s2);
     
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

牛客633875330号:投不了,相当于空白简历,建议看看其他人简历上的项目和实习经历再说,。非科班无项目无实习,拿什么投计算机?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务