We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
string caesarCipher(string s, int k)
{
string res("");
int ch = 0;
for (auto c : s)
{
if (isalpha(c))
{
int ch = int(c);
ch += k;
/* check where we are in relation to 1 -26 , taking into
account of any Caps letter
*/
int curr_index = isupper(c) ? (int(c) -65) : int(c) - 97;
/* by how much are we going to be out of bound*/
bool out_of_boundOffset = (k + curr_index > 25);
if ( out_of_boundOffset )
ch = isupper(c) ? (65 + (k + curr_index) % 26) : (97 + (k + curr_index) % 26);
res.push_back(char(ch));
}
else {
res.push_back(c);
}
}
return res;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Caesar Cipher
You are viewing a single comment's thread. Return to all comments →
here is my simple C++ solution // // //
string caesarCipher(string s, int k) { string res(""); int ch = 0;
}