import java.util.Scanner; class RicMain { public static void main(String args[]) { { Scanner rc = new Scanner(System.in); System.out.print("Input a string: "); char[] letters = rc.nextLine().toCharArray();//When inputting, what happens then? System.out.print("Reverse string: "); for (int i = letters.length - 1; i >= 0; i--)//What is happening inside the for loop? { System.out.print(letters[i]); } System.out.print("\n"); } } }
Please help me understand this code. I had to find the solution online because I didn't know where to begin, so I got this code, tried it on my Java and it worked, but I have trouble understanding it. So After I input my name, how does the for loop reverse it?
I know rc.nextLine() is where I input characters, but when I add toCharArray(), does it convert that array into a string or something?
Please explain how toCharArray() works.