Java

How to Reverse a String in Java Without Using String.reverse()

In Java, there can be instances where the developer needs to utilize the contained data differently. For instance, checking for a “palindrome” or updating the data using the available data. In such case scenarios, reversing a string in Java is of great aid in utilizing the current resources and using the palindrome values effectively.

This article will discuss the approaches to reversing a string without utilizing the “String.reverse()” method.

How to Reverse a String Without Using “String.reverse()” in Java?

For reversing a string in Java without using the discussed method, utilize the following approaches:

Approach 1: Reverse a String Via “for” Loop and “charAt()” Method in Java

The “for” loop is used to iterate through all the contained elements, and the “charAt()” method gives the character at the specified index within the string. These approaches can be combined to reversely iterate through the specified string characters and append it to a separate “String” variable.

Syntax

charAt(ind)

In the above-given syntax:

  • ind” refers to the character index.

Example

Let’s overview the below-provided example:

String givenString = "Java";

System.out.println("The given string is: "+givenString);

String reverseString = "";

System.out.println("The reversed string is: ");

for (int i= (givenString.length()-1);i>=0;i--) {

reverseString= reverseString + givenString.charAt(i);

System.out.println(reverseString);

}

In this code, apply the below-provided steps:

  • Firstly, initialize the String value and display it.
  • Also, allocate a separate String variable to accumulate the reversed string.
  • After that, apply the “for” loop to iterate through the string characters reversely via the “length” property.
  • Lastly, append the reversed string one by one with the help of the associated “charAt()” method and display the reversed string.

Output

In the above output, it can be observed that the default string is reversed character by character due to the applied “for” loop.

Approach 2: Reverse a String Using the “StringBuilder” Class in Java

The Java “StringBuilder” class creates an editable succession of characters. The “reverse()” method reverses the string characters, and the “toString()” method gives the string representation of an object. These combined approaches can be implemented to create a “StringBuilder” object, reverse the passed string and transform it into a string again.

Example

The below-provided example can be utilized to reverse a string via “object”:

String givenString = "Java";

System.out.println("The given string is: "+givenString);

StringBuilder output = new StringBuilder(givenString);

givenString = output.reverse().toString();

System.out.println("The reversed string is: " + givenString);

In the above code block:

  • Likewise, initialize a String value and log it on the console.
  • After that, create a “StringBuilder” object using the “new” keyword and the “StringBuilder()” constructor, respectively, and pass the initialized string in it.
  • Now, associate the combined “reverse()” and “toString()” methods to reverse the linked object and transform it into a string again.
  • Finally, display the resultant reversed string value.

Output

Approach 3: Reverse a String in Java Via “Recursion” Technique

In this particular approach, a string can be reversed by making a function call itself before being invoked in the main with the help of the “charAt()” method.

Example

Let’s go through the following example:

public class reversestring2 {

public static void reverseString(String x, int index) {

if (index >=0) {

System.out.print(x.charAt(index));

reverseString(x, index - 1);

}}

public static void main(String[] args) {

String givenString = "Java";

System.out.println("The given string is: "+givenString);

System.out.println("The reversed string is: ");

reverseString(givenString, givenString.length()-1);

}}

In the above code block:

  • First of all, define a function named “reverseString()”.
  • The former function parameter points to the string that needs to be reversed. The latter parameter corresponds to the indexes of the string characters.
  • In the function definition, iterate through the passed string characters indexes via the “charAt()” method provided that the index initiates from “0” in the “if” condition.
  • Make a function call by passing the string and iterating reversely.
  • In the “main”, initialize and display the string value.
  • Lastly, invoke the function by passing the initialized string, and the latter parameter specifies the string length.
  • Note that “1” is subtracted from the string’s length since the index starts from “0”.

Output

In the above outcome, it can be analyzed that the initialized string is reversed successfully.

Conclusion

To reverse a string in Java without using the “String.reverse()” method, apply the combined “for” loop and “charAt()” method, the “StringBuilder” Class, or the “Recursion” approach. These approaches reverse the specified string by iterating through it, via object, or by making a function call itself before being invoked in the main. This blog discussed the approaches to reversing a string in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.