forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
67 lines (52 loc) · 1.31 KB
/
solution.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
/*
What is asked convert the given string to specific string and increase the value by three
Note - Uppercase letters can be any where in string so we need to keep track of that also
1st LOOP
checks all uppercase letters and convert them to lowercase
2nd Loop
now incrementing all alphabets by three
3rd Loop
changes lowercase alphabets to uppercase
*/
#include <bits/stdc++.h>
using namespace std;
string caesarCipher(string s, int k) {
int n=s.length();
// to keep track of capital letters initially
vector<bool>mod(n,false);
for(int i=0; i<n; i++){
if(s[i]>=65&&s[i]<91){
mod[i]=true;
s[i]=s[i]+32;
}
}
//Incrementing all alphabets by three
for(int i=0; i<n; i++){
if(s[i]>=97&&s[i]<=122){
if(s[i]-97+k>25){
s[i]=97+((s[i]-97+k)%26);
}else{
s[i]=s[i]+k;
}
}
}
// now converting those lowercase alphabets to uppercase those were uppercase initally
for(int i=0; i<n; i++){
if(mod[i]){
s[i]=s[i]-32;
}
}
return s;
}
int main()
{
int n;
cin >> n;
string s;
cin>>s;
int k;
cin >> k;
string result = caesarCipher(s, k);
cout << result << "\n";
return 0;
}