题解 | #字符串加解密#C++解法,就根据题目意思写就行了
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include<bits/stdc++.h> using namespace std; int jiami(string str1)//加密 { for(auto &x:str1)//auto &x是为了引用遍历,不然值不会改变 { if(x>='a'&&x<='z') { if(x=='z') x='A'; else x=toupper(x)+1; continue; } if(x>='A'&&x<='Z') { if(x=='Z') x='a'; else x=tolower(x)+1; continue; } if(x>='0'&&x<='9') { if(x=='9') x='0'; else x=x+1; continue; } } cout<<str1<<endl; return 0; } int jiemi(string str2)//解密 { for(auto &x:str2)//auto &x是为了引用遍历,不然值不会改变 { if(x>='a'&&x<='z') { if(x=='a') x='Z'; else x=toupper(x)-1; continue; } if(x>='A'&&x<='Z') { if(x=='A') x='z'; else x=tolower(x)-1; continue; } if(x>='0'&&x<='9') { if(x=='0') x='9'; else x=x-1; continue; } } cout<<str2<<endl; return 0; } int main() { string str1,str2; while(cin>>str1>>str2) { jiami(str1); jiemi(str2); } }