Reverse a string in java

Let’s learn to reverse a string in java.

Reverse a string in java

Let’s learn following ways to reverse a string.

As we know that String is immutable. String class do not have reverse() method. While StringBuffer class has reverse() method.

reverse a string in java

First let’s learn to reverse a string using ByteArray.

// java program to reverse a string
class ReverseStringByteArray
{
   public static void main(String[] args)
   {
      String input = "HelloWorld";
      // getBytes() method to convert string into bytes[].
      byte[] strByteArray = input.getBytes();
      byte[] output = new byte[strByteArray.length];
      // store output in reverse order
      for(int a = 0; a < strByteArray.length; a++)
         output[a] = strByteArray[strByteArray.length - a - 1];
      System.out.println(new String(output));
   }
}


Output:

dlroWolleH


reverse string using reverse() method of StringBuilder class

// stringbuilder reverse
class ReverseUsingReverseMethod
{
   public static void main(String[] args)
   {
      String str = "Hello world Java";
      StringBuilder sb = new StringBuilder();
      // append string to StringBuilder
      sb.append(str);
      sb = sb.reverse();
      // printing reversed String
      System.out.println(sb);
   }
}


Output:

avaJ dlrow olleH


reverse string using character array

class ReverseUsingCharacterArray
{
   public static void main(String[] args)
   {
      String str = "HelloWorldJava";
      char[] ch = str.toCharArray();
      for(int a = ch.length - 1; a >= 0; a--)
         System.out.print(ch[a]);
   }
}


Output:

avaJdlroWolleH


reverse string using toCharArray() method

class ReverseUsingToCharArrayMethod
{
   public static void main(String[] args)
   {
      String str = "Hello World Java";
      char[] chTemp = str.toCharArray();
      int left, right = 0;
      right = chTemp.length - 1;
      for(left = 0; left < right ; left++, right--)
      {
         // swap values
         char temp = chTemp[left];
         chTemp[left] = chTemp[right];
         chTemp[right]=temp;
      }
      for(char c : chTemp)
         System.out.print(c);
      System.out.println();
   }
}


Output:

avaJ dlroW olleH


reverse string using ArrayList object – Collections.reverse() method

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
class ReverseUsingArrayList
{
   public static void main(String[] args)
   {
      String str = "Hello World Java";
      char[] ch = str.toCharArray();
      List<Character> al = new ArrayList<>();
      for(char c: ch)
         al.add(c);
      Collections.reverse(al);
      ListIterator<Character> li = al.listIterator();
      while(li.hasNext())
         System.out.print(li.next());
   }
}


Output:

avaJ dlroW olleH


reverse using for loop

To reverse a string using for loop, variable ‘b’ is iterated through for loop from b = length of the given string to b greater than 0.

Then in the next step character of string is printed at index (b – 1). Lastly print reversed string.

Here’s the program to reverse a string in java using for loop.

// reverse a string in java using for loop
import java.util.Scanner;
public class ReverseStringForLoop 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string to reverse: ");
      String strInput = sc.nextLine();    
      System.out.println("Reversed string using for loop '" + strInput + "' is: "); 
      for(int b = strInput.length(); b > 0; --b)
      {
         System.out.println(strInput.charAt(b - 1)); 
      }
      sc.close();
   }
}


Output:

Please enter a string to reverse: helloworld
Reversed string using for loop ‘helloworld’ is: dlrowolleh

Please enter a string to reverse: flowerbrackets
Reversed string using for loop ‘flowerbrackets’ is: stekcarbrewolf


This is all about reversing a string in java. In this post we discussed few simple ways in reversing a string in java.

Hope this post would be helpful for interview for freshers in java.