Skip to content
Advertisement

How do I keep spaces in a string?

I am working with a substitution cipher, where each letter of the alphabet is represented by a letter from the substituted alphabet.

JavaScript

The output I’m expecting is 'pl m', however I am getting 'plo' as the space moves to the next letter since there isn’t a space in the substituted alphabet. Is there a way to preserve that space without using regex?

Advertisement

Answer

If the letter is in your alphabet, you add the encrypted letter. But if it’s not in the alphabet, you don’t do anything. You should still add it, just not encrypted:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement