题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <iostream>
using namespace std;
void encrypt(string& str) {
int len = str.length();
for (int i = 0; i < len; i++) {
// 大写字母
if (str[i] >= 'A' && str[i] <= 'Z') {
if (str[i] == 'Z') {
str[i] = 'a';
} else {
str[i] = tolower(str[i]) + 1;
}
} else if (str[i] >= 'a' && str[i] <= 'z') {
if (str[i] == 'z') {
str[i] = 'A';
} else {
str[i] = toupper(str[i]) + 1;
}
} else {
if (str[i] == '9') {
str[i] = '0';
} else {
str[i] = (str[i]) + 1;
}
}
}
return;
}
void decrypt(string& str) {
int len = str.length();
for (int i = 0; i < len; i++) {
// 大写字母
if (str[i] >= 'A' && str[i] <= 'Z') {
if (str[i] == 'A') {
str[i] = 'z';
} else {
str[i] = tolower(str[i]) - 1;
}
} else if (str[i] >= 'a' && str[i] <= 'z') {
if (str[i] == 'a') {
str[i] = 'Z';
} else {
str[i] = toupper(str[i]) - 1;
}
} else {
if (str[i] == '0') {
str[i] = '9';
} else {
str[i] = (str[i]) - 1;
}
}
}
return;
}
int main() {
string encryptStr;
string decryptStr;
cin >> encryptStr;
cin >> decryptStr;
encrypt(encryptStr);
decrypt(decryptStr);
cout << encryptStr << endl;
cout << decryptStr << endl;
return 0;
}
// 64 位输出请用 printf("%lld")

