题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <iostream>
using namespace std;
string encrypt(string s1) {
string res;
for (auto c : s1) {
if (c >= 'a' && c < 'z') {
res += toupper(c) + 1;
}
if (c >= 'A' && c < 'Z') {
res += tolower(c) + 1;
}
if (c == 'z') {
res += 'A';
}
if (c == 'Z') {
res += 'a';
}
if (c >= '0' && c < '9') {
res += c + 1;
}
if (c == '9') {
res += '0';
}
}
return res;
}
string decrypt(string s2) {
string res;
for (auto c : s2) {
if (c > 'a' && c <= 'z') {
res += toupper(c) - 1;
}
if (c > 'A' && c <= 'Z') {
res += tolower(c) - 1;
}
if (c == 'a') {
res += 'Z';
}
if (c == 'A') {
res += 'z';
}
if (c > '0' && c <= '9') {
res += c - 1;
}
if (c == '0') {
res += '9';
}
}
return res;
}
int main() {
string s1, s2;
while (cin >> s1 >> s2) {
cout << encrypt(s1) << endl << decrypt(s2) << endl;
}
}
#23届找工作求助阵地##我的实习求职记录##14天打卡计划##零基础学习C++#
查看1道真题和解析