题解 | #字符串加解密#C++解法,就根据题目意思写就行了

字符串加解密

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

#include<bits/stdc++.h>
using namespace std;
int jiami(string str1)//加密
{
    for(auto &x:str1)//auto &x是为了引用遍历,不然值不会改变
    {
        if(x>='a'&&x<='z')
        {
            if(x=='z') x='A';
            else x=toupper(x)+1;
            continue;
        }
        if(x>='A'&&x<='Z')
        {
            if(x=='Z') x='a';
            else x=tolower(x)+1;
            continue;
        }
        if(x>='0'&&x<='9')
        {
            if(x=='9') x='0';
            else x=x+1;
            continue;
        }
    }
    cout<<str1<<endl;
    return 0;
}
int jiemi(string str2)//解密
{
    for(auto &x:str2)//auto &x是为了引用遍历,不然值不会改变
    {
        if(x>='a'&&x<='z')
        {
            if(x=='a') x='Z';
            else x=toupper(x)-1;
            continue;
        }
        if(x>='A'&&x<='Z')
        {
            if(x=='A') x='z';
            else x=tolower(x)-1;
            continue;
        }
        if(x>='0'&&x<='9')
        {
            if(x=='0') x='9';
            else x=x-1;
            continue;
        }
    }
    cout<<str2<<endl;
    return 0;
}
int main()
{
    string str1,str2;
    while(cin>>str1>>str2)
    {
        jiami(str1);
        jiemi(str2);
    }
}
全部评论

相关推荐

头像
03-18 09:09
Java
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务