题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
void encoder(string& str) {
int N = str.size();
for (int i = 0; i < N; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
if (str[i] == 'z') {
str[i] = 'A';
continue;
}
str[i] = toupper(str[i] + 1);
} else if (str[i] >= 'A' && str[i] <= 'Z') {
if (str[i] == 'Z') {
str[i] = 'a';
continue;
}
str[i] = tolower(str[i] + 1);
} else if (str[i] >= '0' && str[i] <= '9') {
if (str[i] == '9') {
str[i] = '0';
continue;
}
str[i] = str[i] + 1;
}
}
}
void decoder(string& str) {
int N = str.size();
for (int i = 0; i < N; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
if (str[i] == 'a') {
str[i] = 'Z';
continue;
}
str[i] = toupper(str[i] - 1);
} else if (str[i] >= 'A' && str[i] <= 'Z') {
if (str[i] == 'A') {
str[i] = 'z';
continue;
}
str[i] = tolower(str[i] - 1);
} else if (str[i] >= '0' && str[i] <= '9') {
if (str[i] == '0') {
str[i] = '9';
continue;
}
str[i] = str[i] - 1;
}
}
}
int main() {
string Needprotect;
string passProtected;
cin >> Needprotect >> passProtected;
encoder(Needprotect);
decoder(passProtected);
cout << Needprotect << endl << passProtected;
}
// 64 位输出请用 printf("%lld")
模拟
#华为机试#华为OD机测试题 文章被收录于专栏
个人练习专栏

