-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.cpp
45 lines (43 loc) · 1.42 KB
/
caesar.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
#include <iostream>
using namespace std;
int main(){
string c;
char n[50];
int k;
char mod;
cout << "What do you want to do enc(e)/dec(d): ";
cin >> mod;
if (mod == 'e'){
cout << "Enter a message to be encrypted: ";
cin.ignore();
getline(cin, c);
cout << "Enter Key: ";
cin >> k;
for (int i=0; i < c.size();i++){
if (c[i] != ' '){
n[i] = char(int(c[i]) + k); // transformam caracterul in cod ASCII, rezultatul il adunam cu cheia si il transformam inapoi in caracter
}
else{
n[i] += ' '; // In caz ca este spatiu il lasam neschimbat (in caz ca vrem sa encriptam si spatiul scoatem if-ul
}
}
cout << "Encrypted message: " << n << endl;
}
else{
cout << "Enter a message to be decrypted: ";
cin.ignore();
getline(cin, c);
cout << "Enter Key: ";
cin >> k;
for (int i = 0; i < c.size();i++){
if (c[i] != ' '){
n[i] = char(int(c[i]) - k);// transformam caracterul in cod ASCII, rezultatul il scadem cu cheia si il transformam inapoi in caracter
}
else{
n[i] += ' ';// In caz ca este spatiu il lasam neschimbat (in caz ca vrem sa encriptam si spatiul scoatem if-ul
}
}
cout << "Decrypted message: " << n << endl;
}
return 0;
}