0
Why my following code isn't working
Problem: Write a program to take a string as input and output its reverse. The given code takes a string as input and converts it into a char array, which contains letters of the string as its elements. Sample Input: hello there Sample Output: ereht olleh My Code: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); //your code goes here for(int i = arr.length; i >= 0; i--) { System.out.println(char[i]); } } }
1 Answer
+ 2
Subtract 1 from arr.length so that i is equal to the last index of the array instead of 1 beyond.