0

Unable to find error! In reverse string program.

https://code.sololearn.com/c4SF77RtT538/?ref=app

11th May 2021, 12:31 PM
Ganesh Kumar Nayak
Ganesh Kumar Nayak - avatar
5 Answers
+ 1
https://code.sololearn.com/ch2EV908N5mU/?ref=app here you didnt put -1, so its out of bounds
11th May 2021, 12:55 PM
senz
senz - avatar
+ 1
You are getting an index out of range because you directly used the length of your table as an index. The length of "asd" is 3, however, the indexes starts at 0, meaning that the max index is 2! You can fix it by adding a -1 in there: System.out.print(arr[Ttl-1]);
11th May 2021, 12:58 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
Thank You idk and Apollo-Roboto
11th May 2021, 1:21 PM
Ganesh Kumar Nayak
Ganesh Kumar Nayak - avatar
- 1
yep, its like python and c++
11th May 2021, 1:01 PM
senz
senz - avatar
- 1
Here's the corrected code: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char a[] = sc.nextLine().toCharArray(); for (int i=a.length-1;i>=0;i--) { System.out.print(a[i]); } } } // Hope this helps
11th May 2021, 2:50 PM
Calvin Thomas
Calvin Thomas - avatar