-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrot13_cipher.cpp
More file actions
57 lines (46 loc) · 2.12 KB
/
rot13_cipher.cpp
File metadata and controls
57 lines (46 loc) · 2.12 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
#include <iostream>
using namespace std;
int main()
{
// Variable declarations
char character_1, character_2, character_3;
// I assign a negative position which I will need to control the character at the end
int position_character_1 = -1, position_character_2 = -1, position_character_3 = -1;
// three different input
cout << "Write first character: ";
cin >> character_1;
cout << "Write second character: ";
cin >> character_2;
cout << "Write third character: ";
cin >> character_3;
// I declare the variables that I will need to calculate the position of the letter with ROT13
char rot13_position_character_1, rot13_position_character_2, rot13_position_character_3;
// Check that the letters entered are lowercase
// The -1 is replaced if the character is correct
// Subtract the - 'a' character to get the ASCII position
// From the ASCII position I add 13 for the ROT13 and use Modulo to restart the calculation if it exceeds the number 26
if (character_1 >= 'a' && character_1 <= 'z')
{
position_character_1 = character_1 - 'a';
rot13_position_character_1 = (position_character_1 + 13) % 26;
}
// repeat operations
if (character_2 >= 'a' && character_2 <= 'z')
{
position_character_2 = character_2 - 'a';
rot13_position_character_2 = (position_character_2 + 13) % 26;
}
// repeat operations
if (character_3 >= 'a' && character_3 <= 'z')
{
position_character_3 = character_3 - 'a';
rot13_position_character_3 = (position_character_3 + 13) % 26;
}
// Finally print the result.
// If the value remained -1 it means that the character inserted did not pass the checks
// Otherwise add 'a' or 97 in ASCII to the original position, this way the compiler prints the exact character
cout << (position_character_1 == -1 ? '?' : char(rot13_position_character_1 + 'a')) << endl;
cout << (position_character_2 == -1 ? '?' : char(rot13_position_character_2 + 'a')) << endl;
cout << (position_character_3 == -1 ? '?' : char(rot13_position_character_3 + 'a')) << endl;
return 0;
}