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.
publicstaticStringcaesarCipher(Strings,intk){k=k%26;// Reduce k to avoid unnecessary shiftsStringBuildersb=newStringBuilder();for(charc:s.toCharArray()){// Use char directly for simplicityif(c>='A'&&c<='Z'){// Uppercase letterssb.append((char)('A'+(c-'A'+k)%26));}elseif(c>='a'&&c<='z'){// Lowercase letterssb.append((char)('a'+(c-'a'+k)%26));}else{// Non-alphabetic characterssb.append(c);}}returnsb.toString();}
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 →
Simple Java Solution: