题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <iostream> #include <vector> #include <string> using namespace std; int main() { string s; getline(cin, s); // 输入第一个字符串 string s1; getline(cin, s1); // 输入第二个字符串 vector<char> str; // 对第一个字符串进行转换 for (auto& ch : s) { if ('a' <= ch && ch <= 'z') { // 小写字母变为大写并移动 if (ch == 'z') { str.push_back('A'); } else { str.push_back(ch - 'a' + 'A' + 1); } } else if ('A' <= ch && ch <= 'Z') { // 大写字母变为小写并移动 if (ch == 'Z') { str.push_back('a'); } else { str.push_back(ch - 'A' + 'a' + 1); } } else if ('0' <= ch && ch <= '9') { // 数字字符加 1 if (ch == '9') { str.push_back('0'); } else { str.push_back(ch + 1); } } else { // 其他字符保持不变 str.push_back(ch); } } // 输出第一个字符串转换后的结果 for (int i = 0; i < str.size(); i++) { cout << str[i]; } cout << endl; vector<char> astr; // 对第二个字符串进行逆向转换 for (const char& ch : s1) { if ('a' <= ch && ch <= 'z') { // 小写字母变为大写并向前移动 if (ch == 'a') { astr.push_back('Z'); } else { astr.push_back(ch - 'a' + 'A' - 1); } } else if ('A' <= ch && ch <= 'Z') { // 大写字母变为小写并向前移动 if (ch == 'A') { astr.push_back('z'); } else { astr.push_back(ch - 'A' + 'a' - 1); } } else if ('0' <= ch && ch <= '9') { // 数字字符减 1 if (ch == '0') { astr.push_back('9'); } else { astr.push_back(ch - 1); } } else { // 其他字符保持不变 astr.push_back(ch); } } // 输出第二个字符串转换后的结果 for (int i = 0; i < astr.size(); i++) { cout << astr[i]; } cout << endl; return 0; } // 64 位输出请用 printf("%lld")