题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <string> #include <iostream> using namespace std; string encode(string& s){ string res; for(char c:s){ if(c>='a'&&c<='z'){ res+=(c-'a'+1)%26+'A'; }else if(c>='A'&&c<='Z'){ res+=(c-'A'+1)%26+'a'; }else if(c>='0'&&c<='9'){ res+=(c-'0'+1)%10+'0'; } } return res; } string decode(string& s){ string res; for(char c:s){ if(c>='a'&&c<='z'){ if(c-'a'==0) res+='Z'; else res+=(c-'a'-1)%26+'A'; }else if(c>='A'&&c<='Z'){ if(c-'A'==0) res+='z'; else res+=(c-'A'-1)%26+'a'; }else{ if(c=='0') res+='9'; else res+=(c-'0'-1)%10+'0'; } } return res; } int main() { string s1,s2; getline(cin,s1); getline(cin,s2); cout<<encode(s1)<<endl; cout<<decode(s2)<<endl; }