题目描述:
1、对输入的字符串进行加解密,并输出。
2加密方法为:
当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
其他字符不做变化。
3、解密方法为加密的逆过程。
输入:
输入一串要加密的密码
输入一串加过密的密码
abcdefg
BCDEFGH
输出:
输出加密后的字符
输出解密后的字符
BCDEFGH
abcdefg
做题思路:
思路1. 按照转换规则对每个字符进行转换,比如加密过程:
char c = str[i];
if( str[i]>='a'&&str[i]<'z' ){
c = str[i]-31;
}
if( str[i]=='z' ){
c = 'A';
}
if( str[i]>='A'&&str[i]<'Z' ){
c = str[i]+33;
}
if( str[i]=='Z' ){
c = 'a';
}
if( str[i]>='0'&&str[i]<'9' ){
c = str[i]+1;
}
if( str[i]=='9' ){
c = '0';
}
思路2. 把转换后和转换好的表先对应准备好
const string helper1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const string helper2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
AC代码:
#include<iostream>
#include<string>
using namespace std;
void Encrypt(string str); //对字符串进行加密
void unEncrypt(string str); //对字符串进行解密
int main()
{
string str1,str2;
while( cin>>str1>>str2 ){
Encrypt(str1);
unEncrypt(str2);
}
return 0;
}
void Encrypt(string str)
{
string password; //加密后的字符串
for(int i=0; i<str.size(); i++){
char c = str[i];
if( str[i]>='a'&&str[i]<'z' ){
c = str[i]-31;
}
if( str[i]=='z' ){
c = 'A';
}
if( str[i]>='A'&&str[i]<'Z' ){
c = str[i]+33;
}
if( str[i]=='Z' ){
c = 'a';
}
if( str[i]>='0'&&str[i]<'9' ){
c = str[i]+1;
}
if( str[i]=='9' ){
c = '0';
}
password.push_back(c);
}
cout<<password<<endl;
}
void unEncrypt(string str)
{
string initial; //解密后的字符串
for(int i=0; i<str.size(); i++){
char c = str[i];
if( str[i]>'a'&&str[i]<='z' ){
c = str[i]-33;
}
if( str[i]=='a' ){
c = 'Z';
}
if( str[i]>'A'&&str[i]<='Z' ){
c = str[i]+31;
}
if( str[i]=='A' ){
c = 'z';
}
if( str[i]>'0'&&str[i]<='9' ){
c = str[i]-1;
}
if( str[i]=='0' ){
c = '9';
}
initial.push_back(c);
}
cout<<initial<<endl;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)