How to reverse a string in java | Sololearn: Learn to code for FREE!

+2

How to reverse a string in java

5/24/2021 1:38:23 PM

Godfred Opintan

11 Answers

New Answer

+9

Godfred Opintan , since this question has been asked frequently, please use the search bar. thanks!

+5

use array- https://code.sololearn.com/cy26M72V2D2g/?ref=app StringBuilder https://code.sololearn.com/cpAv2CZejx67/?ref=app

+4

https://code.sololearn.com/cUNFGY80MjfR/?ref=app Reverse of a String

+2

+2

Reverse it by decreasing .length()-1 function in the loop

+1

You can also do it this way without StringBuilder..... https://code.sololearn.com/cOF9nbaxaytB/?ref=app

+1

That's short. thanks

+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]); } } }

0

//it was quite simple 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]); } } }

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