题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <iostream> #include <vector> #include <set> #include <unordered_set> #include <map> #include <algorithm> #include <string> using namespace std; int main(){ string str1, str2; cin >> str1 >> str2; // 加密 for(int i =0;i<str1.size();++i){ char c = str1[i]; // 判断字母 if(isalpha(c)){ if (c >= 'a' && c <= 'z') { c = 'a' + (c - 'a'+1) % 26; c = toupper(c); } else { c = 'A' + (c - 'A' + 1) % 26; c = tolower(c); } }else if(isdigit(c)){ c = '0' + (c - '0' + 1) % 10; } str1[i] = c; } // 解密 for (int i = 0; i < str2.size(); ++i) { char c = str2[i]; // 判断字母 if (isalpha(c)) { if (c >= 'a' && c <= 'z') { c = 'z' - ('z' - c + 1) % 26; c = toupper(c); } else { c = 'Z' - ('Z' - c + 1) % 26; c = tolower(c); } }// 判断数字 else if (isdigit(c)) { c = '9' - ('9' - c + 1) % 10; } str2[i] = c; } cout << str1 << endl; cout << str2 << endl; }