Hi, for reversing a strin, whats wrong with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 } }

11th Nov 2021, 5:40 AM
Narges Abbasi
2 Answers
+ 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.
11th Nov 2021, 6:02 AM
Brian
Brian - avatar
+ 1
Thanks
11th Nov 2021, 6:22 AM
Narges Abbasi