华为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<iostream>
using namespace std;
#include<string>
//字符串加密
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<<result1[i];
	        i++;
	}
	cout<<endl;
     //解密输出
   uncrypt (passWord2, result2);
   i=0;
   while(result2[i]!='\0')
   {     cout<<result2[i];
         i++;
   }
	return 0;
}
VS平台的实验结果如图所示,在OJ平台也可顺利通过!为方便OJ平台上直接通过,显示比较直接。

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值