Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Want to keep spaces between words when encrypting in Java using Caesar Cipher

When typing a message (e.g. I love Java) the encrypted version comes out without the spaces. So if a input shift value to be 0, the output will be ilovejava, but I want to keep the spaces between the words.
Can you please help me and thank you.

import java.util.Scanner;

public class CaesarCipherHW
{
    public static final String Alphabet = " abcdefghijklmnopqrstuvwxyz";
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a message: "); //prompts the user to enter the message that will be encrypted/decrypted
        String message = sc.nextLine();
        message = message.toLowerCase(); //converts the message to lower case characters only

        System.out.println("Enter shift value (between 0-25): "); //prompts user to enter the value by which each character will be shifted
        int shift = sc.nextInt();

        System.out.println("1- Encrypt" +
                            "\n2- Decrypt" +
                            "\n3- Exit" +
                            "\nChoose your option: "); //ask user to choose what the program will execute

    }

     public static class CaesarCipher
    {
        String cipher(String message, int shift)
        {
            String encryptedMessage = ""; //variable to store encrypted message

            for (int i = 0; i < message.length(); i++) //use for loop to go through all the letters in the
                                                        // message that are also contained in the alphabet variable
            {
                if (message.charAt(i) == ' ') continue;

                int charPos = Alphabet.indexOf(message.charAt(i)); //find index of each letter in the alphabet
                int encryptPos = (shift + charPos) % 26; //find which index of the Alphabet to use to encrypt the letter in the message

                if (encryptPos < 0)
                {
                    encryptPos = Alphabet.length() + encryptPos;
                }

                char replaceChar = Alphabet.charAt(encryptPos); //use encryptPos variable to replace each letter in the message

                encryptedMessage += replaceChar; //add the encrypted character to the variable that stores the encrypted string


            }

            return encryptedMessage;
        }
    }

    
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your cypher implementation is explicitly skipping spaces:

if (message.charAt(i) == ' ') continue;

Instead of skipping them, you can reinsert them into the output:

if (message.charAt(i) == ' ') {
    encryptedMessage += ' ';
    continue;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading