Skip to content
Advertisement

Caesar Cipher Uppercase and Lowercase Wrap Around

I am having a problem regarding wrapping around the alphabet with my Caesar Cipher Program.

The program works fine with all lowercase letters. Wraps around perfectly and is able to apply positive and negative shifts. When i try to input an uppercase letter, the uppercase letter does not wrap around.

Here is my code:

JavaScript

}

Hints would be much appreciated! Of course, I am not asking anyone to do my work for me but some help would be appreciated! Thank you! 🙂

Edit: here’s the if statement that does the wrap around for lowercase letters only:

JavaScript

Advertisement

Answer

Let’s implement a wrap-around function for a single character. This will be used by the other method. When you separate your tasks and sub-tasks wisely, you will observe that your problem becomes easier to solve. Here I have based my solution on the fact that char variables are represented as numbers, we know the number of letters and we can use that number as a modulo class, to make sure that algebra is aiding us.

JavaScript

Now, instead of

JavaScript

You need just:

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