Java reverse string

Hello everyone!! Welcome to flower brackets blog. Here we are going to learn java reverse string.

Java reverse string

In java a string can be reversed in ‘n’ number of ways. Below are few examples.

import java.util.Scanner;

public class ReverseStringDemo 
{
   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();
      String reverseString = ""; 

      for(int a = strInput.length() - 1; a >= 0; a--)
      {
         reverseString = reverseString + strInput.charAt(a);
      }

      System.out.println("Reversed string : ");
      System.out.println(reverseString);
      sc.close();
   }
}

Output :

Please enter a string to reverse : flower brackets
Reversed string : stekcarb rewolf

In the above java program we are reading string from user. Then, to get reversed string we iterate through for loop.

For loop loops characters from the string entered by user from the end using charAt() function of String class and concatenating to new string using + operator.


Using reverse() method

Here we are using built in reverse() method of StringBuilder class,

import java.util.Scanner;

public class ReverseJavaExample 
{
   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(strInput);
      System.out.println("Reversed string : ");
      System.out.println(sb.reverse().toString());
      sc.close();
   }
}

Output:

Please enter a string to reverse : java
Reversed string : avaj


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.

import java.util.Scanner;

public class StringReverseDemo 
{
   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();

      for(int a = strInput.length() - 1; a >= 0; a--)
      {
         sb.append(strInput.charAt(a));
      }

      System.out.println("Output : ");
      System.out.println(sb.toString());
      sc.close();
   }
}

Output:

Please enter a string to reverse : flower brackets
Output : stekcarb rewolf


Convert String into Bytes

First we create a temporary byte array equal to length of given string.

Also read – sort string array java

Next we create temporary byte array in which we store bytes which we get using getBytes() method in reverse order. Lastly a new String object is created to store the result.

public class StringReversal 
{
   public static void main(String[] args) 
   {
      String strInput = "Flowerbrackets";

      byte[] strByte = strInput.getBytes();

      byte[] result = new byte[strByte.length];

      for(int a = 0; a < strByte.length; a++)
         result[a] = strByte[strByte.length - a - 1];

      System.out.println("Output : " + new String(result));
   }
}

Output :

Output : stekcarbrewolF


Convert String to character array

In this, 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";

      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";
      char demo[] = strInput.toCharArray(); // converting strInput into character 
array using toCharArray()
      int left, right = 0; // setting left index and right index equal to 0
      right = demo.length - 1; // setting length of string - 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";
      char[] demo = strInput.toCharArray(); // copying String to ArrayList object
      List<Character> test = new ArrayList<>();

      for(char ch : demo)
         test.add(ch);

      Collections.reverse(test); // reverse() method takes "test" object, 
to reverse the list 
      ListIterator li = test.listIterator(); // creating ListIterator object 
using the listIterator() method on LinkedList object
      while(li.hasNext()) // iterate over the list
         System.out.print(li.next()); // printing it one by one
   }
}

Output :

stekcarB rewolF


Bottom Line : Above examples on how to reverse a string in java can be used in java based on project requirement.


Related Posts