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.
c++ solution
this solution uses vector to store the new position of charecters after shiftting it by k element then the corrosponding indexs are retrive to build the cipher text
{stringcipher_text="";vector<char>alphabet(26);for(inti=0;i<26;++i){alphabet[i]='a'+(i+k)%26;}for(inti=0;i<s.size();i++){charc=s[i];boolf=false;// edge case of uppercase we have to convert it to lowercase first then after finding the corresponding //chsr make it uppercase againif(c>='A'&&c<='Z'){c=tolower(s[i]);f=true;}intidx=c-'a';if(idx>=0&&idx<=25){if(f){cipher_text+=toupper(alphabet[idx]);}else{cipher_text+=alphabet[idx];}}else{cipher_text+=c;}}returncipher_text;}
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 →
c++ solution this solution uses vector to store the new position of charecters after shiftting it by k element then the corrosponding indexs are retrive to build the cipher text