File tree Expand file tree Collapse file tree 1 file changed +81
-0
lines changed Expand file tree Collapse file tree 1 file changed +81
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments