Skip to content

Commit 4c96d9f

Browse files
Merge pull request #244 from PulkitKhagta/master
Added code for CaesarCipher in C++
2 parents 4348a8e + e6ff288 commit 4c96d9f

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

CaesarCipher/C++/caesarcipher.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using namespace std;
2+
3+
int main()
4+
{
5+
char message[100], ch;
6+
int i, key, n;
7+
8+
cout<< "Press 1 to encrypt the message"<<endl;
9+
cout<< "Press 2 to decrypt the message" <<endl;
10+
11+
cin>>n;
12+
if(n == 1)
13+
{
14+
cout << "Enter a message to encrypt: ";
15+
cin.getline(message, 100);
16+
cout << "Enter key: ";
17+
cin >> key;
18+
19+
for(i = 0; message[i] != '\0'; ++i){
20+
ch = message[i];
21+
22+
if(ch >= 'a' && ch <= 'z'){
23+
ch = ch + key;
24+
25+
if(ch > 'z'){
26+
ch = ch - 'z' + 'a' - 1;
27+
}
28+
29+
message[i] = ch;
30+
}
31+
else if(ch >= 'A' && ch <= 'Z'){
32+
ch = ch + key;
33+
34+
if(ch > 'Z'){
35+
ch = ch - 'Z' + 'A' - 1;
36+
}
37+
38+
message[i] = ch;
39+
}
40+
}
41+
42+
cout << "Encrypted message: " << message;
43+
}
44+
else if(n == 2)
45+
{
46+
cout << "Enter a message to decrypt: ";
47+
cin.getline(message, 100);
48+
cout << "Enter key: ";
49+
cin >> key;
50+
51+
for(i = 0; message[i] != '\0'; ++i){
52+
ch = message[i];
53+
54+
if(ch >= 'a' && ch <= 'z'){
55+
ch = ch - key;
56+
57+
if(ch < 'a'){
58+
ch = ch + 'z' - 'a' + 1;
59+
}
60+
61+
message[i] = ch;
62+
}
63+
else if(ch >= 'A' && ch <= 'Z'){
64+
ch = ch - key;
65+
66+
if(ch > 'a'){
67+
ch = ch + 'Z' - 'A' + 1;
68+
}
69+
70+
message[i] = ch;
71+
}
72+
}
73+
74+
cout << "Decrypted message: " << message;
75+
}
76+
else
77+
{
78+
cout<< "Entered number neither 1 nor 2"<<endl;
79+
}
80+
return 0;
81+
}

0 commit comments

Comments
 (0)