1

I tried to bring back removed white space after encrypted string.

Enter Plain Text:hello world
Result : itssgvgkbsr

It should bring back the removed spaces into their actual places.Expected Output:

Result : itssg gkbsr

  private static void EncryptionChiper(String plainText, String keyChiper){
    char[] letter = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'r', 'x', 'y', 'z'};
    char[] charKeyChiper = keyChiper.toCharArray();
    System.out.print("Result : ");
    for (int i = 0;i<plainText.length();i++){
        for (int j = 0;j<letter.length;j++){
            if(letter[j]==(plainText.charAt(i))){
                System.out.print(charKeyChiper[j]);

            }
        }
    }
}

 System.out.print("Enter Plain Text:");String plainText = scan.nextLine();
 System.out.print("Enter Key(26):");String key = scan.nextLine();
 EncryptionChiper(plainText,key);
1
  • 2
    How did you try to preserve the white-spaces? I don't see any case or code that would handle it. Commented Mar 11, 2020 at 11:04

1 Answer 1

2

Just add another if case:

if(' '==(plainText.charAt(i))){
   System.out.print(' ');
   break;
} else if(letter[j]==(plainText.charAt(i))){
   System.out.print(charKeyChiper[j]);
   break;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Genius! You beat me to it.
it worked but i don't know why?my code print too much white space between the word
Yes I just not noticed the way you are looping. I'm editing the answer adding breaks for nested for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.