描述
对输入的字符串进行加解密,并输出。
加密方法为:
当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母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进行初始化