-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0029.cpp
63 lines (59 loc) · 1.24 KB
/
0029.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 0029.字符串加解密
#include <iostream>
#include <cstring>
using namespace std;
string encode(string s, string r)
{
r = "";
char c;
for(int i = 0; i < s.size(); i++)
{
if(isalpha(s[i])) {
if(isupper(s[i])) {
c = tolower(s[i]) + 1;
if(c > 'z') c = 'a';
} else {
c = toupper(s[i]) + 1;
if(c > 'Z') c = 'A';
}
}else if(isdigit(s[i])){
c = s[i] + 1;
if(c > '9') c = '0';
}
r += c;
}
return r;
}
string decode(string s, string r)
{
r = "";
char c;
for(int i = 0; i < s.size(); i++)
{
if(isalpha(s[i])) {
if(isupper(s[i])) {
c = tolower(s[i]) - 1;
if(c < 'a') c = 'z';
} else {
c = toupper(s[i]) - 1;
if(c < 'A') c = 'Z';
}
}else if(isdigit(s[i])){
c = s[i] - 1;
if(c < '0') c = '9';
}
r += c;
}
return r;
}
int main()
{
string s1, s2, r;
while(getline(cin, s1))
{
getline(cin, s2);
cout << encode(s1, r) << endl;
cout << decode(s2, r) << endl;
}
return 0;
}