题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <cctype> #include <iostream> using namespace std; void add_(string &str){ for (int i = 0; i < str.size(); i++){ if (isalpha(str[i])){ if (str[i] == 'Z') str[i] = 'a'; else if(str[i] == 'z') str[i] = 'A'; else if (isupper(str[i])) str[i] = tolower(str[i]) + 1; else str[i] = toupper(str[i]) + 1; } else{ if (str[i] == '9') str[i] = '0'; else str[i] = str[i] + 1; } } } void readd_(string &str){ for (int i = 0; i < str.size(); i++){ if (isalpha(str[i])){ if (str[i] == 'A') str[i] = 'z'; else if(str[i] == 'a') str[i] = 'Z'; else if (isupper(str[i])) str[i] = tolower(str[i]) - 1; else str[i] = toupper(str[i]) - 1; } else{ if (str[i] == '0') str[i] = '9'; else str[i] = str[i] - 1; } } } int main() { string str_1, str_2; cin>>str_1>>str_2; add_(str_1); readd_(str_2); cout << str_1<<endl<<str_2; } // 64 位输出请用 printf("%lld")