How to reverse a string in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to reverse a string in java

24th May 2021, 1:38 PM
Godfred Opintan
Godfred Opintan - avatar
8 Answers
+ 9
Godfred Opintan , since this question has been asked frequently, please use the search bar. thanks!
24th May 2021, 1:46 PM
Lothar
Lothar - avatar
25th May 2021, 8:27 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 2
Thanks
24th May 2021, 1:47 PM
Godfred Opintan
Godfred Opintan - avatar
+ 2
Reverse it by decreasing .length()-1 function in the loop
26th May 2021, 6:59 AM
Atul [Inactive]
+ 1
You can also do it this way without StringBuilder..... https://code.sololearn.com/cOF9nbaxaytB/?ref=app
26th May 2021, 10:26 AM
Jan
Jan - avatar
+ 1
That's short. thanks
26th May 2021, 7:05 PM
Godfred Opintan
Godfred Opintan - avatar
+ 1
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(); for(int i=arr.length-1;i>=0;i--) { System.out.println(arr[i]); } } }
14th Jun 2021, 4:21 PM
Dharmi Sri
Dharmi Sri - avatar
0
for(int i = arr.length - 1; i >= 0; i--){ System.out.print(arr[i]); yup, my issue, why it didn´t work. Was separating by comma and not semi colon in the for condition. " , } ) " these three are really a pain in the ass sometimes, lol
23rd Sep 2021, 12:55 PM
Dario Poddighe
Dario Poddighe - avatar