C++的一道加密题目

来源:百度知道 编辑:UC知道 时间:2024/06/07 22:36:42
加密算法为:A->X,a->x
B->Y,b->y
编写一个加密函数encrypt,及解密函数decrypt,
要求1、把字串:this is an apple,翻译为加密后字串
2、把字串:R droo erhrg HszmtSzr翻译为明文
用字符数组及string各编程一次

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

void encrypt(char *str)
{
for (int i=0; i<strlen(str); i++)
{
if ('A'<str[i] && str[i]<'Z')
{
str[i] = str[i] + 23;
if (str[i]>'Z') str[i] = str[i]-26;
}
if ('a'<str[i] && str[i]<'z')
{
str[i] = str[i] + 23;
if (str[i]>'z') str[i] = str[i]-26;
}
}
cout<<"加密结果:"<<str<<endl;
}

void decrypt(char *str)
{
for (int i=0; i<strlen(str); i++)
{
if ('A'<str[i] && str[i]<'Z')
{
if(str[i]-23>0) str[i]=str[i]-23;
else str[i] = str[i]+3;
}
}
cout<<"解密结果:"<<str<<endl;
}

void main()
{
char str[50];
cout<<"输入要加密的字符串:";
c