0

import java.util.Scanner; //import

public class project2 {

public static void main(String[] args) {
    
   
    Scanner in = new Scanner(System.in); //setting scanner
    System.out.println("Please enter the text you wish to encode."); //question
    String place = in.nextLine();
    
    place = place.replace('a', 'z'); //Making what letters replace with what
    place = place.replace('b', 'y');
    place = place.replace('c', 'x');
    place = place.replace('d', 'w');
    place = place.replace('e', 'v');
    place = place.replace('f', 'u');
    place = place.replace('g', 't');
    place = place.replace('h', 's');
    place = place.replace('i', 'r');
    place = place.replace('j', 'q');
    place = place.replace('k', 'p');
    place = place.replace('l', 'o');
    place = place.replace('m', 'n');
    place = place.replace('n', 'm');
    place = place.replace('o', 'l');
    place = place.replace('p', 'k');
    place = place.replace('q', 'j');
    place = place.replace('r', 'i');
    place = place.replace('s', 'h');
    place = place.replace('t', 'g');
    place = place.replace('u', 'f');
    place = place.replace('v', 'e');
    place = place.replace('w', 'd');
    place = place.replace('x', 'c');
    place = place.replace('w', 'b');
    place = place.replace('z', 'a');
    
    System.out.println(place); //typing the encoded message
}

}

This is what I have so far, but only the bottom half is working, the rest isn't replacing.

Daniel
  • 1
  • whats the test case you used and whats the result? From what I see, the code after replacing the top half, it is later replaced by the bottom half (ie A-> Z, it detects Z, converts Z-> A) – experiment unit 1998X May 04 '22 at 03:21
  • also, you have a typo for y -> b – experiment unit 1998X May 04 '22 at 03:21
  • you could consider a string or char[] of a to z, whenever you iterate over the char in the string, you check the char[], get index. check for equivalent cipher index from char[] length - 1 - index, and replace each char in the string with the cipher char – experiment unit 1998X May 04 '22 at 03:24
  • Replace your letters with Upper case letters then use the **String#toLowerCase()** method to convert everything to lowercase. – DevilsHnd - 退職した May 04 '22 at 03:27
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 04 '22 at 16:50

1 Answers1

1

So taking a look at this, I decided to send the following through your code.

"abcdefghijklmnopqrstuvwxyz" 

In response, I got

"aycdefghijklmmlkjihgfedcya".

Which is almost what I would expect. What I would expect is

"abcdefghijklmmlkjihgfedcba". 

This leads me to the first problem.

place = place.replace('w', 'b');

That 'w' should be a 'y'.

Now the second problem is the one you're more specifically asking about. Why does it mirror at m? Well, we are going through this string sequentially and updating it after each change. So when we get to m a-l has been changed to z-n. So as we continue through your code we will be changing all of the characters that were once a-l back to a-l.

Instead, try this answer https://stackoverflow.com/a/45668235/12572585 I've tested it and it works fine.