题目链接
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a?tpId=37&tqId=21252&tPage=2&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking
题目描述
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个字符。
输入描述:
输入说明
输入一串要加密的密码
输入一串加过密的密码
输出描述:
输出说明
输出加密后的字符
输出解密后的字符
示例1
输入
复制
abcdefg
BCDEFGH
输出
复制
BCDEFGH
abcdefg
题解:
#include <iostream>
#include <string>
using namespace std;
void Encrypt (string s, string &a){
for (int i = 0; i < s.length(); ++i){
if (s[i] >= 'a' && s[i] < 'z'){
a[i] = s[i] - 31;
}
else if (s[i] == 'z'){
a[i] = 'A';
}
if (s[i] >= 'A' && s[i] < 'Z'){
a[i] = s[i] + 33;
}
else if (s[i] == 'Z'){
a[i] = 'a';
}
if (s[i] >= '0' && s[i] < '9'){
a[i] = s[i] + 1;
}
else if (s[i] == '9'){
a[i] = '0';
}
}
}
void unEncrypt (string s, string &a){
for (int i = 0; i < s.length(); ++i){
if (s[i] > 'a' && s[i] <= 'z'){
a[i] = s[i] - 33;
}
else if (s[i] == 'a'){
a[i] = 'Z';
}
if (s[i] > 'A' && s[i] <= 'Z'){
a[i] = s[i] + 31;
}
else if (s[i] == 'A'){
a[i] = 'z';
}
if (s[i] > '0' && s[i] <= '9'){
a[i] = s[i] - 1;
}
else if (s[i] == '0'){
a[i] = '9';
}
}
}
int main(){
string a, b, c, d;
while (cin >> a) {
cin >> b;
c = a;
d = b;
Encrypt(a, c);
unEncrypt(b, d);
cout << c << endl << d << endl;
}
return 0;
}