-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2_1_CaesarCipher.cpp
More file actions
144 lines (100 loc) · 3.77 KB
/
Task2_1_CaesarCipher.cpp
File metadata and controls
144 lines (100 loc) · 3.77 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
/*
FCI-Programming 1 -2018 - Assignment 2
Program Name : caesar cipher.
Last Modification Date 3/3/2018.
Author1 and ID and Group : Fares sayed hassan , 20170191 , 8.
Author2 and ID and Group : Alaa Ibrahem , 20170166 , 7.
Author3 and ID and Group : Amr Alaa , 20170187 , 7.
Author4 and ID and Group : Ali Samir , 20170172 ,7.
Teaching Assistant:
Purpose : ciphering and deciphering using caesar way.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
char ciepher (char character, int shift);
char deciepher(char character, int shift);
int main()
{
int choice ;
cout << "hello user! what do you want?" << endl;
string message;
string ciepheredMessage;
string deciepheredMessage;
int shifts;
while(true){
cout << "\npress 1 for ciephering\n";
cout << "press 2 for deciephering\n";
cout << "press any other number to exit\n";
cin >> choice;
//ciephering
if(choice == 1){
cout << "enter number of shifts:\n";
cin >> shifts;
cin.ignore();
cout << "enter your message:\n";
getline(cin,message);
for(int i=0;i<message.length();i++){
if(message[i]==' '){
ciepheredMessage+=' ';
}else{
ciepheredMessage+=ciepher(message[i],shifts);
}
}//end of ciephering loop
cout << "ciepher: "<< ciepheredMessage;
// Deciephering.
}else if(choice == 2){
cout << "enter number of shifts:\n";
cin >> shifts;
cin.ignore();
cout << "enter your message:\n";
getline(cin,message);
for(int i=0;i<message.length();i++){
if(message[i]==' '){
deciepheredMessage+=' ';
}else{
deciepheredMessage+=deciepher(message[i],shifts);
}
}//end of deciephering loop.
cout << "deciepher: " << deciepheredMessage;
}else{
break;
}
}
return 0;
}// end of main().
char ciepher(char character ,int shift){
int assciNumberOfCeipherc=0;
int assciToConvertc=0;
if(character>='a' && character<='z'){
assciNumberOfCeipherc=character+shift;
assciToConvertc=assciNumberOfCeipherc-(assciNumberOfCeipherc >'z'?26:0);
return (char(assciToConvertc));
}else if(character>='A' && character<='Z'){
assciNumberOfCeipherc=character+shift;
assciToConvertc=assciNumberOfCeipherc-(assciNumberOfCeipherc >'Z'?26:0);
return (char(assciToConvertc));
}else{
assciNumberOfCeipherc=character+shift;
assciToConvertc=assciNumberOfCeipherc;
return (char(assciToConvertc));
}
}
char deciepher(char character,int shift){
int assciNumberOfDeceipherd=0;
int assciToConvertd=0;
if(character>='a' && character<='z'){
assciNumberOfDeceipherd=character-shift;
assciToConvertd=assciNumberOfDeceipherd+(assciNumberOfDeceipherd <'a'?26:0);
return (char(assciToConvertd));
}else if(character>='A' && character<='Z'){
assciNumberOfDeceipherd=character-shift;
assciToConvertd=assciNumberOfDeceipherd+(assciNumberOfDeceipherd <'A'?26:0);
return (char(assciToConvertd));
}else{
assciNumberOfDeceipherd=character-shift;
assciToConvertd=assciNumberOfDeceipherd;
return (char(assciToConvertd));
}
}