题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <iostream>
#include <string>
using namespace std;
void encrypting(string& unencrypted){
for(int i = 0; i < unencrypted.size(); ++i){
if(unencrypted[i] >= 'a' && unencrypted[i] <= 'z'){
if(unencrypted[i] == 'z'){
unencrypted[i] = 'A';
}
else{
unencrypted[i] = (char)toupper(unencrypted[i] + 1);
}
}
else if(unencrypted[i] >= 'A' && unencrypted[i] <= 'Z'){
if(unencrypted[i] == 'Z'){
unencrypted[i] = 'a';
}
else{
unencrypted[i] = (char)tolower(unencrypted[i] + 1);
}
}
else if(unencrypted[i] >= '0' && unencrypted[i] <= '9'){
if(unencrypted[i] == '9'){
unencrypted[i] = '0';
}
else{
unencrypted[i] = unencrypted[i] + 1;
}
}
}
}
void unencrypting(string& encrypted){
for(int i = 0; i < encrypted.size(); ++i){
if(encrypted[i] >= 'a' && encrypted[i] <= 'z'){
if(encrypted[i] == 'a'){
encrypted[i] = 'Z';
}
else{
encrypted[i] = (char)toupper(encrypted[i] - 1);
}
}
else if(encrypted[i] >= 'A' && encrypted[i] <= 'Z'){
if(encrypted[i] == 'A'){
encrypted[i] = 'z';
}
else{
encrypted[i] = (char)tolower(encrypted[i] - 1);
}
}
else if(encrypted[i] >= '0' && encrypted[i] <= '9'){
if(encrypted[i] == '0'){
encrypted[i] = '9';
}
else{
encrypted[i] = encrypted[i] - 1;
}
}
}
}
int main(int argc, char* argv[]){
string unencrypted;
getline(cin, unencrypted);
string encrypted;
getline(cin, encrypted);
encrypting(unencrypted);
unencrypting(encrypted);
cout << unencrypted << endl;
cout << encrypted << endl;
return 0;
}