题解 | #字符串加解密#

字符串加解密

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

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

void JiaMi(string str)
{
    int length = str.length();
    for(int i=0; i<length; i++)
    {
        //小写字母
        if(str[i]-'a'>=0 &&str[i]-'z'<0)
        {
            str[i] -= 31;
        }
        else if(str[i] == 'z')
        {
            str[i] = 'A';
        }
        //大写字符
        else if(str[i]-'A'>=0 &&str[i]-'Z'<0)
        {
            str[i] += 33;
        }
        else if(str[i] == 'Z')
        {
            str[i] = 'a';
        }
        //数字
        else if(str[i]-'0'>=0 &&str[i]-'9'<0)
        {
            str[i] += 1;
        }
        else if(str[i] == '9')
        {
            str[i] = '0';
        }
    }
    cout<<str<<endl;
}
void JieMi(string str)
{
    int length = str.length();
    for(int i=0; i<length; i++)
    {
        //小写字母
        if(str[i]-'a'>0 &&str[i]-'z'<=0)
        {
            str[i] -= 33;
        }
        else if(str[i] == 'a')
        {
            str[i] = 'Z';
        }
        //大写字符
        else if(str[i]-'A'>0 &&str[i]-'Z'<=0)
        {
            str[i] += 31;
        }
        else if(str[i] == 'A')
        {
            str[i] = 'z';
        }
        //数字
        else if(str[i]-'0'>0 &&str[i]-'9'<=0)
        {
            str[i] -= 1;
        }
        else if(str[i] == '0')
        {
            str[i] = '9';
        }
    }
    cout<<str<<endl;
}
int main()
{
    string str1, str2;
    while(cin>>str1>>str2)
    {
        JiaMi(str1);
        JieMi(str2);
    }
    return 0;
}
全部评论

相关推荐

水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
评论
3
2
分享

创作者周榜

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