-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar Cipher.cpp
101 lines (72 loc) · 2.08 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
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
/*
Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +3: defghijklmnopqrstuvwxyzabc
For example, the given cleartext and the alphabet is rotated by . The encrypted string is .
Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.
Function Description
Complete the caesarCipher function in the editor below. It should return the encrypted string.
caesarCipher has the following parameter(s):
s: a string in cleartext
k: an integer, the alphabet rotation factor
Input Format
The first line contains the integer, , the length of the unencrypted string.
The second line contains the unencrypted string, .
The third line contains , the number of letters to rotate the alphabet by.
Constraints
is a valid ASCII string without any spaces.
Output Format
For each test case, print the encoded string.
Sample Input
11
middle-Outz
2
Sample Output
okffng-Qwvb
Explanation
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +2: cdefghijklmnopqrstuvwxyzab
m -> o
i -> k
d -> f
d -> f
l -> n
e -> g
- -
O -> Q
u -> w
t -> v
z -> b
*/
#include <bits/stdc++.h>
using namespace std;
// Complete the caesarCipher function below.
string caesarCipher(string s, int k) {
//string ans(s.length(),'4');
char a;
for(auto &c :s)
{
if(isalpha(c))
{
a =isupper(c) ? 'A' : 'a';
c=(char)a+((c-a)+k)%26;
}
}
return s;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string s;
getline(cin, s);
int k;
cin >> k;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string result = caesarCipher(s, k);
fout << result << "\n";
fout.close();
return 0;
}