Caesar Cipher Encryptor
Encode and decode messages with the time-honored Caesar Cipher, adapting ancient wisdom to modern-day programming.
Published At
6/4/2021
Reading Time
~ 2 min read
Given a non-empty string of lowercase letters and a non-negative integer representing a key, write a function that returns a new string obtained by shifting every letter in the input string by k positions in the alphabet, where k is the key.
Note that letters should "wrap" around the alphabet; in other words, the letter z shifted by one returns the letter a.
Sample Input
string = "xyz"
key = 2
string = "xyz"
key = 2
Sample Output
"zab"
"zab"
Hints
Hint 1
Most languages have built-in functions that give you the Unicode value of a character as well as the character corresponding to a Unicode value. Consider using such functions to determine which letters the input string's letters should be mapped to.
Hint 2
Try creating your own mapping of letters to codes. In other words, try associating each letter in the alphabet with a specific number - its position in the alphabet, for instance - and using that to determine which letters the input string's letters should be mapped to.
Hint 3
How do you handle cases where a letter gets shifted to a position that requires wrapping around the alphabet? What about cases where the key is very large and causes multiple wrappings around the alphabet? The modulo operator should be your friend here.
Optimal Space & Time Complexity
O(n) time | O(n) space - where n is the length of the input string
function caesarCipherEncryptor(string, key) {
let newString = ''
key = key % 26;
for (let i = 0; i < string.length; i++) {
let newAlpha = string.charCodeAt(i) + key;
if (newAlpha > 122) {
let howMuchGreater = newAlpha - 122;
newAlpha = 97 + howMuchGreater - 1;
}
newString += String.fromCharCode(newAlpha)
}
return newString;
}
function caesarCipherEncryptor(string, key) {
let newString = ''
key = key % 26;
for (let i = 0; i < string.length; i++) {
let newAlpha = string.charCodeAt(i) + key;
if (newAlpha > 122) {
let howMuchGreater = newAlpha - 122;
newAlpha = 97 + howMuchGreater - 1;
}
newString += String.fromCharCode(newAlpha)
}
return newString;
}
đż
Do you have any questions, or simply wish to contact me privately? Don't hesitate to shoot me a DM on Twitter.
Have a wonderful day.
Abhishek đ