华为OJ_2128_字符串加解密

来源:互联网 发布:淘宝美工兼职怎么样 编辑:程序博客网 时间:2024/05/16 05:30

接口描述:
实现接口,每个接口实现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()可以恢复strEncrypt( 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