[华为OJ--C++]023-字符串加解密

题目描述:对输入的字符串进行加解密,并输出。(字符串中只含有英文字母和数字)

加密方法为:当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;解密方法为加密的逆过程。

输入描述:输入一串要加密的密码和一串加过密的密码

输出描述:输出加密后的字符和解密后的字符

 输入例子:

abcdefg

BCDEFGH

输出例子:

BCDEFGH

abcdefg

算法实现:

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

/************************************************ 
 * Author: 赵志乾 
 * Date: 2017-2-17  
 * Declaration: All Rigths Reserved !!! 
 ***********************************************/ 

int main()
{
	string code,decode;
	cin>>code>>decode;

	string retcode=code;
	for(int i=0;i<code.length();i++)
	{
		if(code[i]>='A'&&code[i]<='Z')
		{
			retcode[i]=(code[i]-'A'+1)%26+'a';
		}
		else if(code[i]>='a'&&code[i]<='z')
		{
			retcode[i]=(code[i]-'a'+1)%26+'A';
		}
		else
		{
			retcode[i]=(code[i]-'0'+1)%10+'0';
		}
	}

	string retdecode=decode;
	for(int i=0;i<decode.length();i++)
	{
		if(decode[i]>='A'&&decode[i]<='Z')
		{
			retdecode[i]=(decode[i]-'A'-1+26)%26+'a';
		}
		else if(decode[i]>='a'&&decode[i]<='z')
		{
			retdecode[i]=(decode[i]-'a'-1+26)%26+'A';
		}
		else
		{
			retdecode[i]=(decode[i]-'0'-1+10)%10+'0';
		}
	}
	
	cout<<retcode<<endl;
	cout<<retdecode<<endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫白小猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值