0
Hi, for reversing a strin, whats wrong with this 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(); int i; for (i=0;i<= arr.lenght;i++); { int x = arr.length -i; } System.out.println(arr[x]); //your code goes here } }
2 Antworten
+ 2
In the for() loop condition, arr.length is misspelled.
The printing of each character is outside the for() loop.
The first calculation of x is outside the bounds of the array. To fix this, initialize i = 1 instead of 0.
+ 1
Thanks