-
Notifications
You must be signed in to change notification settings - Fork 1
/
Caesar Cipher.cpp
68 lines (67 loc) · 1.32 KB
/
Caesar Cipher.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
64
65
66
67
68
#include<iostream>
#include<string.h>
using namespace std;
int main() {
char con='y';
char msg[100];
int i, j, length,choice,key;
do{
cout<<"Enter the message:\n";
cin>>msg;
cout << "Enter key: ";
cin >> key;
length = strlen(msg);
cout<<"Enter your choice \n1. Encryption \n2. Decryption \n";
cin>>choice;
if (choice==1)
{
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
if (ch >= 'a' && ch <= 'z'){
ch = ch + key;
if (ch > 'z'|| ch<'a') {
ch = ch - 'z' + 'a' - 1;
}
msg[i] = ch;
}
else if (ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if (ch > 'Z'|| ch<'A'){
ch = ch - 'Z' + 'A' - 1;
}
msg[i] = ch;
}
}
cout<<"Encrypted message: "<< msg;
}
else if (choice == 2) {
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
if(ch >= 'a' && ch <= 'z') {
ch = ch - key;
if(ch < 'a'||ch>'b'){
ch = ch + 'z' - 'a' + 1;
}
msg[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z') {
ch = ch - key;
if(ch < 'A'||ch>'Z') {
ch = ch + 'Z' - 'A' + 1;
}
msg[i] = ch;
}
}
cout << "Decrypted message: " << msg;
}
else
{
cout<<"Enter valid choice.";
}
cout<<"\nDo you wanna continue: if yes press Y or y otherwise any character:";
cin>>con;
}
while(con=='y'|| con=='Y');
}