-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRot13.cpp
More file actions
78 lines (65 loc) · 1.52 KB
/
Copy pathRot13.cpp
File metadata and controls
78 lines (65 loc) · 1.52 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <string>
#include <map>
#include <iostream>
using namespace std;
string rot13(string msg)
{
if (msg.empty())
return "";
map<char, int> res;
int i = 1;
for (char ch = 'a'; ch <= 'z'; ch++)
res[ch] = i++;
map<int, char> res2;
int k = 1;
for (char ch = 'a'; ch <= 'z'; ch++)
res2[k++] = ch;
k = 50;
for (char ch = 'A'; ch <= 'Z'; ch++)
res2[k++] = ch;
i = 50;
for (char ch = 'A'; ch <= 'Z'; ch++)
res[ch] = i++;
string temp;
for (auto j : msg)
{
if (res[j] >= 1 && res[j] <= 13)
temp += res2[res[j] + 13];
else if (res[j] >= 14 && res[j] <= 26)
temp += res2[res[j] - 13];
else if (res[j] >= 50 && res[j] <= 62)
temp += res2[res[j] + 13];
else if (res[j] >= 63 && res[j] <= 75)
temp += res2[res[j] - 13];
else
temp += j;
}
return temp;
}
//BEST CODE
string Rot13_best(string text)
{
string res = "";
for (auto& i : text)
{
if (isalpha(i))
{
if (islower(i))
res += 'a' + (i - 'a' + 13) % 26;
else if (isupper(i))
res += 'A' + (i - 'A' + 13) % 26;
}
else
res += i;
}
return res;
}
int main()
{
cout << rot13("test") << endl;
cout << rot13("Test") << endl;
cout << rot13("AbCd") << endl;
cout << Rot13_best("test") << endl;
cout << Rot13_best("Test") << endl;
cout << Rot13_best("AbCd") << endl;
}