题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <cctype> #include <iostream> #include <string> using namespace std; string& encrypt(string& s) { for (char& c : s) { if (isalpha(c)) { // 字母 if (c >= 'a' && c <= 'z') { c = c == 'z' ? 'a' : c + 1; c = toupper(c); } else { c = c == 'Z' ? 'A' : c + 1; c = tolower(c); } } else { // 数字 if (c != '9') c += 1; else c = '0'; } } return s; } string& decrypt(string& s) { for (char& c : s) { if (isalpha(c)) { // 字母 if (c >= 'a' && c <= 'z') { c = c == 'a' ? 'z' : c - 1; c = toupper(c); } else { c = c == 'A' ? 'Z' : c - 1; c = tolower(c); } } else { if (c != '0') c -= 1; else c = '9'; } } return s; } int main() { string str1, str2; cin >> str1 >> str2; cout << encrypt(str1) << endl << decrypt(str2) << endl; } // 64 位输出请用 printf("%lld")