All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Reverse A String In Java

Last Updated : Mar 11, 2024

How To Reverse A String In Java

In this article we will show you the solution of how to reverse a string in java, you can start by utilising the getBytes() function to turn the input string into a byte array in order to reverse a string in Java.

Once you have the byte array, you can duplicate it with a new byte array that is the same size.

By copying each byte into the new byte array at the appropriate point, you can then iterate through the original byte array in reverse order.

Finally, you can use the String constructor, which accepts a byte array as input, to change the inverted byte array back into a string.

This method is more effective for larger strings since it allows you to reverse the string without having to go character by character through it.

Step By Step Guide On How To Reverse A String In Java :-

import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString {
 public static void main(String[] args)
 {
  String input = "Talkerscode";
  byte[] strAsByteArray = input.getBytes();
  byte[] result = new byte[strAsByteArray.length];
  for (int i = 0; i < strAsByteArray.length; i++)
   result[i] = strAsByteArray[strAsByteArray.length - i - 1];
  System.out.println(new String(result));
 }
}
  1. You can see that we have written a Java programme that uses the getBytes() function to show how to reverse a string.
  2. ReverseString, a class that has a main method that acts as the program's entry point, is the first thing we declare when we begin the programme.
  3. A string input is initialized with the value "Talkerscode" inside the main procedure.
  4. The input string is then transformed into a byte array using the getBytes() method, which is then saved in the strAsByteArray variable.
  5. The platform's default charset is used to translate the string into a series of bytes using this approach.
  6. The next step is to build a new byte array called result with the same size as strAsByteArray.
  7. Then, using a for loop, the original byte array is iterated through in reverse order, copying each byte to the new byte array at its appropriate position.
  8. This is accomplished by accessing and allocating the byte to result[i] at index strAsByteArray.length - i - 1.
  9. The inverted byte array is then transformed back into a string using the new String() constructor, which is then written to the console by means of the System.out.println() function.
  10. The program produces the string "edocskreT," which is the opposite of the initial string "Talkerscode."

Conclusion :-

As a result, we have successfully learnt the Java notion of string reversal.

We also discovered that Java's getBytes() method may be used to transform a string to a byte array, which can then be used in a variety of ways.

This program demonstrated how to use the getBytes() method to reverse a string by converting a string into a byte array, iterating over the array of bytes in reverse order, and then converting the reverse byte array back into a string.

This method is effective because it avoids having to traverse through the string character by character.

The programme shows off Java's flexibility in handling various forms of data by giving an easy example of how to work with byte arrays.

I hope this article on how to reverse a string in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪