华为OJ_2128_字符串加解密

接口描述:
实现接口,每个接口实现1个基本操作:
void Encrypt (char aucPassword[], char aucResult[])
:在该函数中实现字符串加密并输出
说明:
1、字符串以\0结尾。
2、字符串最长100个字符。
 
int unEncrypt (char result[], char password[])
:在该函数中实现字符串解密并输出
说明:
1、字符串以\0结尾。
2、字符串最长100个字符。
样例输入:
abcdefg
BCDEFGH
                   
样例输出:
BCDEFGH
abcdefg


#include <iostream>
#include <string>
#include <cctype>

using namespace std;

void Encrypt (char aucPassword[], char aucResult[]);
void unEncrypt (char result[], char password[]);

int main( void )
{
	string sPw1, sPw2, sRst1, sRst2;
	cin >> sPw1;
	cin >> sRst2;
	
	// const_cast<char*>会使得string不可用,但是str.c_str()仍然可用
	// str = str.c_str()可以恢复str
	Encrypt( const_cast<char*>( sPw1.c_str() ), const_cast<char*>( sRst1.c_str() ) );
	unEncrypt( const_cast<char*>( sRst2.c_str() ), const_cast<char*>( sPw2.c_str() ) );

	cout << sRst1.c_str() << endl;
	cout << sPw2.c_str() << endl;

	return 0;
}

void Encrypt (char aucPassword[], char aucResult[])
{
	char *pPw = aucPassword;
	char *pRst = aucResult;
	while( *pPw != '\0' ){
		if( islower( *pPw ) ){
			*pRst = ( *pPw - 'a' + 1 ) % 26 + 'a' + ( 'A' - 'a' );
		}else if( isupper( *pPw ) ){
			*pRst = ( *pPw - 'A' + 1 ) % 26 + 'A' + ( 'a' - 'A' );
		}else if( isdigit( *pPw ) ){
			*pRst = ( *pPw - '0' + 1 ) % 10 + '0';
		}else{
			*pRst = *pPw;
		}
		++pPw;
		++pRst;
	}
	*pRst = '\0';
}
void unEncrypt (char result[], char password[])
{
	char *pRst = result;
	char *pPw = password;

	while( *pRst != '\0' ){
		if( islower( *pRst ) ){
			*pPw = ( *pRst - 'a' - 1 + 26 ) % 26 + 'a' + ( 'A' - 'a' );
		}else if( isupper( *pRst ) ){
			*pPw = ( *pRst - 'A' - 1 + 26 ) % 26 + 'A' + ( 'a' - 'A' );
		}else if( isdigit( *pRst ) ){
			*pPw = ( *pRst - '0' - 1 + 10 ) % 10 + '0';
		}else{
			*pPw = *pRst;
		}
		++pRst;
		++pPw;
	}
	*pPw = '\0';
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值