👋
Welcome to my blog!

Caesar Cipher Encryptor

Encode and decode messages with the time-honored Caesar Cipher, adapting ancient wisdom to modern-day programming.

Caesar Cipher Encryptor
Strings
easy

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

js
string = "xyz"
key = 2
js
string = "xyz"
key = 2

Sample Output

js
"zab"
js
"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

Solution-1
js
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;
}
Solution-1
js
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 🙏

Join My Exclusive Newsletter Community

Step into a world where creativity intersects with technology. By subscribing, you'll get a front-row seat to my latest musings, full-stack development resources, and exclusive previews of future posts. Each email is a crafted experience that includes:

  • In-depth looks at my covert projects and musings to ignite your imagination.
  • Handpicked frontend development resources and current explorations, aimed at expanding your developer toolkit.
  • A monthly infusion of inspiration with my personal selection of quotes, books, and music.

Embrace the confluence of words and wonder, curated thoughtfully and sent straight to your inbox.

No fluff. Just the highest caliber of ideas.