-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar cipher.cpp
More file actions
47 lines (39 loc) · 835 Bytes
/
Copy pathCaesar cipher.cpp
File metadata and controls
47 lines (39 loc) · 835 Bytes
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
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
string encrypt(int s, string text)
{
string result = "";
int update;
//traverse text
for(int i=0; i<text.size(); i++){
//apply transformation to each character
//encrypt lower case letter
//result += char(int(text[i] + s -97)%26 +97);
if(isspace(text[i])){
result += text[i];
continue;
}
update= (text[i] - s);
if(update<97){
result += (update + 26);
}
else {
result += update;
}
//return the resulting string
}
return result;
}
//Driver code
int main()
{
int s;
cin >> s;
cin.ignore();
string text;
getline(cin, text);
cout << encrypt(s, text);
return 0;
}