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
Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.
Sample Input
11
middle-Outz
2
Sample Output
okffng-Qwvb
Explanation
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +2: cdefghijklmnopqrstuvwxyzab
- Complete the ‘caesarCipher’ function below.
* - The function is expected to return a STRING.
- The function accepts following parameters:
- 1. STRING s
- 2. INTEGER k
*/
string caesarCipher(string s, int k) {
string str;
for(int i=0;i<s.length();i++)
{
k=k%26;
for(char ch='a';ch <= 'z';ch++) //97-122
{
if(s[i] == ch )
{
if(s[i] + k > 122 ) /
{
s[i]= s[i] + k - 26;
}
else {
s[i] = s[i] + k;
}
break;
}
else if(s[i] == ch-32)
{
if(s[i] + k > 90 )
{
s[i]= s[i] + k - 26;
}
else {
s[i] = s[i] + k ;
}
break;
}
}
}
s[s.length()]='\0';
return s;
}
int main()
{
string n_temp;
getline(cin, n_temp);
int n = stoi(ltrim(rtrim(n_temp)));
string s;
getline(cin, s);
string k_temp;
getline(cin, k_temp);
int k = stoi(ltrim(rtrim(k_temp)));
string result = caesarCipher(s, k);
fout << result << "\n";
fout.close();
return 0;
}