华为OJ题库_字符串加解密

 
  

题目:字符串加解密

题目描述

1、对输入的字符串进行加解密,并输出。

2、加密方法为:

当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;

当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;

其他字符不做变化。

3、解密方法为加密的逆过程。

 

接口描述:

    实现接口,每个接口实现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
C++实现过程如下:

#include
using namespace std;
#include
//字符串加密
void Encrypt (char *aucPassword, char *aucResult)
{
	char *str=aucPassword;
	while(*str!='\0')
	{
		if(isalpha(*str))
	    {
		   if((*str>='a') && (*str<='z'))
              *aucResult=(char)toupper(*str)+1;
		     else
                 *aucResult=(char)tolower(*str)-1;
		   //str++;
	}//if
		if (isdigit(*str))
			*aucResult=*str+1;
		if(isspace(*str))
			*aucResult=*str; 
		str++;
		aucResult++;
	}//while
	*aucResult='\0';	
}
//字符串解密
void uncrypt (char *aucPassword, char *aucResult)
{
	char *str=aucPassword;
	while(*str!='\0')
	{
		if(isalpha(*str))
		{
			if((*str>='a') && (*str<='z'))
				*aucResult=(char)toupper(*str)+1;
			else
				*aucResult=(char)tolower(*str)-1;
			//str++;
		}//if
		else if (isdigit(*str))
			*aucResult=*str-1;
		else 
			//if (isspace(*str))
			   *aucResult=*str; 
		str++;
		aucResult++;
	}//while
	*aucResult='\0';
}
int main()
{
    char passWord[100];
	gets_s(passWord);
	char passWord2[100];
	gets_s(passWord2);
    char result1[100];
	char result2[100];
     // 加密输出
    Encrypt (passWord, result1);
	int i=0;
	while(result1[i]!='\0')
	{     cout<
VS平台的实验结果如图所示,在OJ平台也可顺利通过!为方便OJ平台上直接通过,显示比较直接。

其中第一行为需加密的字符串,第二行为需解密的字符串,第三行为加密过得字符串,第四行为解密过得字符串。

 
  

你可能感兴趣的:(编程C++)