题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
string jiami(string& str)
{
for (int i = 0; i < str.size(); i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
if (str[i] == 'z')
str[i] = 'A';
else
str[i] = toupper(str[i]) + 1;
} else if (str[i] >= 'A' && str[i] <= 'Z') {
if (str[i] == 'Z')
str[i] = 'a';
else
str[i] = tolower(str[i]) + 1;
} else if (isdigit(str[i])) {
if (str[i] == '9')
str[i] = '0';
else
str[i] += 1;
}
}
return str;
}
string jiemi(string& str)
{
for (int i = 0; i < str.size(); i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
if (str[i] == 'a')
str[i] = 'Z';
else
str[i] = toupper(str[i]) - 1;
} else if (str[i] >= 'A' && str[i] <= 'Z') {
if (str[i] == 'A')
str[i] = 'z';
else
str[i] = tolower(str[i]) - 1;
} else if (isdigit(str[i])) {
if (str[i] == '0')
str[i] = '9';
else
str[i] -= 1;
}
}
return str;
}
int main() {
string str1;//加密数据
string str2;//解密数据
while (cin>>str1>>str2)
{
string ans=jiami(str1);
cout << ans << endl;
ans=jiemi(str2);
cout << ans << endl;
}
return 0;
}
