In java , what is the form to inverse a character chain? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

In java , what is the form to inverse a character chain?

I used for (int i = arr.length; i>0;i--){} it don't do anything

18th Nov 2021, 11:41 AM
Gonzalo Hernández Hernández
3 Answers
+ 2
You're fine, just keep practicing and you'll get it right
18th Nov 2021, 12:29 PM
Ipang
+ 5
Well you have an empty loop body there. Why aren't you printing the characters using the index <i>? And remember arrays have zero-based indices, so an array of 10 elements allows 0 ~ 9 as valid index. I suggest you to try like this ... for( int i = arr.length - 1; i >= 0; i-- ) { System.out.print( arr[ i ] ); } Initialize <i> with arr.length - 1 Cause if array's length was 10, the last index we could refer to would be the 9th. And for the loop condition, continue loop while <i> >= 0 cause you'd want to also print the first character (stored at index 0).
18th Nov 2021, 12:13 PM
Ipang
+ 2
Thanks Ipang it was that I was very confused about there
18th Nov 2021, 12:27 PM
Gonzalo Hernández Hernández