Let’s learn to reverse a string in java.

Reverse a string in java
In java a string can be reversed in ‘n’ number of ways. To do that we use StringBuilder, StringBuffer and String class. Below are few examples.
Using append method of StringBuilder class
In java, String class does not have reverse() method. So we must convert the input string to StringBuilder using the append method of StringBuilder class.
Then print the reversed string.
import java.util.Scanner; public class StringReverse { public static void main(String[] args) { System.out.println("Please enter a string to reverse : "); Scanner sc = new Scanner(System.in); String strInput = sc.nextLine(); StringBuilder sb = new StringBuilder(); // appending string into StringBuilder sb sb.append(strInput); // reversing StringBuilder sb sb = sb.reverse(); // printing reversed string System.out.println(sb); sc.close(); } }
Output:
Please enter a string to reverse : flower brackets
stekcarb rewolf
Convert String into Bytes
First we create a temporary byte array equal to length of given string. Next we create temporary byte array in which we store bytes which we get using getBytes() method in reverse order.
Also read – sort string array java
getBytes() method returns the resultant byte array. Lastly a new String object is created to store the output.
public class StringReversal { public static void main(String[] args) { String strInput = "Flowerbrackets"; // convert string into bytes using getBytes() method byte strByte[] = strInput.getBytes(); byte output[] = new byte[strByte.length]; // storing output in reverse order for(int a = 0; a < strByte.length; a++) { output[a] = strByte[strByte.length - a - 1]; } System.out.println("Output : " + new String(output)); } }
Output :
Output : stekcarbrewolF
Convert String to character array
In this java program, we first convert string to character array using String class method toCharArray() and print char one by one.
public class ReversalString { public static void main(String[] args) { String strInput = "FlowerBrackets"; // converting string to character array using toCharArray method char demo[] = strInput.toCharArray(); for(int a = demo.length - 1; a >= 0; a--) { System.out.print(demo[a]); } } }
Output :
stekcarBrewolF
Convert input string to character array using toCharArray() method
Here in this java program we set left index = 0 and right index = length of input string minus 1. Next we swap characters from start to end one by one.
In the for loop we increase left index and right index by one and continue up till <= right.
public class StringReverseJava { public static void main(String[] args) { String strInput = "Flower Brackets"; // converting strInput into character array using toCharArray() char demo[] = strInput.toCharArray(); // setting left index and right index equal to 0 int left, right = 0; // setting length of string - 1 right = demo.length - 1; for(left = 0; left < right ; left++ , right--) { // swapping characters from start index with last index and scanning one by one. char temp = demo[left]; demo[left] = demo[right]; demo[right] = temp; } for(char ch : demo) System.out.print(ch); System.out.println(); } }
Output :
stekcarB rewolF
Using ListIterator
In this java program we convert input string to character array using toCharArray() method. Next we add characters of the array into ArrayList object.
In java we have reverse method of Collections class. This method takes list object to reverse list. Lastly we pass LinkedList object which is type of list of characters,
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.ListIterator; public class StringReverser { public static void main(String[] args) { String strInput = "Flower Brackets"; // copying String to ArrayList object char[] demo = strInput.toCharArray(); List<Character> test = new ArrayList<>(); for(char ch : demo) test.add(ch); // reverse() method takes "test" object, to reverse the list Collections.reverse(test); // creating ListIterator object using the listIterator() method on LinkedList object ListIterator li = test.listIterator(); // iterate over the list while(li.hasNext()) { // printing it one by one System.out.print(li.next()); } } }
Output :
stekcarB rewolF
Bottom Line : Above examples on how to reverse a string in java can be used in java based on project requirement.