字符串加解密例题训练

描述

对输入的字符串进行加解密,并输出。

加密方法为:

当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;

当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;

其他字符不做变化。

解密方法为加密的逆过程。

数据范围:输入的两个字符串长度满足 1≤n≤1000 1≤n≤1000  ,保证输入的字符串都是只由大小写字母或者数字组成

输入描述:

第一行输入一串要加密的密码
第二行输入一串加过密的密码

输出描述:

第一行输出加密后的字符
第二行输出解密后的字符

示例1

输入:

abcdefg
BCDEFGH

输出:

BCDEFGH
abcdefg
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str1,str2;
    cin>>str1;
    cin>>str2;
    string str11=str1;
    string str22=str2;//必须先初始化吗
    //加密
    for(int i=0;i<str1.size();i++)
    {
        //大写字母
        if(str1[i]>='A'&&str1[i]<='Z')
        {
            //如果为Z
            if(str1[i]=='Z')
            {
                str11[i]='a';
            }
            else//65-90A 97-122a
            {
                str11[i]=str1[i]+33;

            }
        }
        //小写
         if(str1[i]>='a'&&str1[i]<='z')
        {
            //如果为Z
            if(str1[i]=='z')
            {
                str11[i]='A';
            }
            else//65-90A 97-122a
            {
                str11[i]=str1[i]-31;

            }
        }

        //数字
        if(str1[i]>='0'&&str1[i]<='9')
        {
            if(str1[i]=='9')
            {
                str11[i]='0';

            }
            else
            {
                str11[i]=str1[i]+1;
            }
        }

    }
    //解密
    for(int i=0;i<str2.size();i++)
    {

        //大写字母  变成前一个的相反形式字母
        if(str2[i]>='A'&&str2[i]<='Z')
        {
            if(str2[i]=='A')
            {
                str22[i]='z';

            }
            else
            {
                str22[i]=str2[i]+31;
            }
        }
        //小写字母
        if(str2[i]>='a'&&str2[i]<='z')
        {
            if(str2[i]=='a')
            {
                str22[i]='Z';

            }
            else
            {
                str22[i]=str2[i]-33;
            }
        }
        //数字
        if(str2[i]>='0'&&str2[i]<='9')
        {
            if(str2[i]=='0')
            {
                str22[i]='9';

            }
            else
            {
                str22[i]=str2[i]-1;
            }
        }

        
    }




    cout<<str11<<endl;
    cout<<str22<<endl;



}

必须对str11,与str22进行初始化

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值