Java reverse string

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

Java reverse string

Java reverse string

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

Also Read – Best Way To Reverse String Array In Java

Lets see how to reverse a string in java in a classical way, that is, without using classes in java,

import java.util.Scanner;

public class ReverseStringJava 
{
   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("Reverse of string in java : ");
      System.out.println(reverseString);
      sc.close();
   }
}

Output :

Please enter a string to reverse : flower brackets
Reverse of string in java : stekcarb rewolf


Reverse function

We can use reverse function of StringBuilder class to reverse a string in java,

Example :

import java.util.Scanner;

public class ReverseJava 
{
   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("Reverse of a string : ");
      System.out.println(sb.reverse().toString());
      sc.close();
   }
}

Output:

Please enter a string to reverse : java
Reverse of a string : avaj


Another way

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 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(); // stringbuilder reverse java

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

      System.out.println("Reverse of string in java : ");
      System.out.println(sb.toString());
      sc.close();
   }
}

Output:

Please enter a string to reverse : flower brackets
Reverse of string in java : stekcarb rewolf


getBytes() method

Now we learn reverse string in java using getBytes() method. Here we create a temporary byte and store in reverse order.

Also Read – Sort String Array Java

Lastly we store result in a new string object. Here’s the code for java reverse a string,

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("Reverse string in java : " + new String(result));
   }
}

Output :

Reverse string in java : stekcarbrewolF


toCharArray() method

Here we are going to learn how to reverse a string in java using toCharArray() method. In this, we first convert string to characters 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


Swapping

Let’s learn how to reverse a string in java by swapping,

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


listIterator() method

In this example we are going to learn how to reverse a string in java using listIterator() method,

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.


conclusion

So this was all about java reverse string. I hope you guys have understood how to reverse a string in java.

You can subscribe to my blog flower brackets if you haven’t already. Do share this post if you like.

You May Also Like