-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (109 loc) · 2.12 KB
/
main.cpp
File metadata and controls
151 lines (109 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<string>
#include<iostream>
using namespace std;
const char SEPARATOR = ',';
const string UC_MODE = "uppercase";
const string LC_MODE = "lowercase";
const string PT_MODE = "puntuaction";
const char NULL_VALUE = 0;
string mode = UC_MODE;
bool isDigitOrSeparator(char c){
int n = c - '0';
return (n >= 0 && n <=9) || (n == -4);
}
string nextMode(){
if(mode == UC_MODE){
return LC_MODE;
}
if(mode == LC_MODE){
return PT_MODE;
}
return UC_MODE;
}
char decode_uppercase(int num){
int code = num % 27;
if (code == 0){
mode = nextMode();
return NULL_VALUE;
}
char modifier = '@';
return code + modifier;
}
char decode_lowercase(int num){
int code = num % 27;
if (code == 0){
mode = nextMode();
return NULL_VALUE;
}
char modifier = '`';
return code + modifier;
}
char decode_punctuaction(int num){
int code = num % 9;
if (code == 0){
mode = nextMode();
return NULL_VALUE;
}
switch (code)
{
case 1:
return '!';
case 2:
return '?';
case 3:
return ',';
case 4:
return '.';
case 5:
return ' ';
case 6:
return ';';
case 7:
return '"';
case 8:
return '\'';
default:
return NULL_VALUE;
}
}
char decode(int num){
if (mode == LC_MODE){
return decode_lowercase(num);
}
if (mode == PT_MODE){
return decode_punctuaction(num);
}
return decode_uppercase(num);
}
void read(string msg){
string num;
const char SEPARATOR = ',';
msg.append({SEPARATOR});
for (int i = 0; i < msg.length(); i++){
char c = msg.at(i);
if(!isDigitOrSeparator(c)){
cout << endl << "The " << c << " character was found, which is not valid for a secret message";
break;
}
if (c == SEPARATOR){
int n = stoi(num);
char decoded = decode(n);
if(decoded != NULL_VALUE){
cout << decoded;
}
num.clear();
} else {
num.append({c});
}
}
cout << endl;
}
int main(int argc, char* argv[]){
if (argc != 2){
cout << "The secret message was not provided" << endl;
return -1;
}
string msg = argv[1];
read(msg);
return 0;
}