Javatpoint Logo
Javatpoint Logo

Reverse String Using Array in Java

A string in Java is a series of characters that may be reversed using an array. Reversing a string entails rearranging the characters of a string in the opposite order. This article will look at various techniques for reversing a text in Java using an array.

Method 1: Making use of a character array

To reverse a string, the easiest technique is to convert it to a character array and then swap the characters at both ends of the array. In Java, you can do the following:

ReverseString1.java

Output:

The original String is Hello World!
 Reversed String is !dlroW olleH

Explanation

The toCharArray() method converts the input string into a character array.

Declare two variables, left and right, to maintain track of the array's beginning and last character, respectively.

To traverse through the array, use a while loop, swapping characters from both ends until the left pointer is larger than or equal to the right pointer.

Return the reversed string using the String(char[] value) constructor to create a new string from the transformed character array.

Method 2: Making Use of StringBuilder

The StringBuilder class is another technique to reverse a string in Java. Here's how to go about it:

ReverseString2.java

Output:

The original String is Hello World!
 Reversed String is !dlroW olleH

Explanation

Please make a new StringBuilder instance and provide the input string to its constructor.

To reverse the contents of a StringBuilder object, use the reverse() function.

Using the toString() method, return the StringBuilder object to a string.

Method 4: Employing a for loop

A simple for loop may also be used to reverse a string. Here's how it's done:

ReverseString.java

Output:

The original String is Hello World!
 Reversed String is !dlroW olleH

Explanation

The toCharArray() method converts the input string into a character array.

Declare a new reversedArray with the same length as the original string.

To traverse over the array, use a for loop to copy the characters from the original array to the reversed array in reverse order.

Return the reversed string using the String(char[] value) constructor to create a new string from the reversed character array.

Method 5- Recursion

Recursion can also be used to reverse a string. Here's how it's done:

ReverseString.java

Output:

The original String is Hello World!
 Reversed String is !dlroW olleH

Explanation

Determine whether the provided string is empty. If it is, the empty string is returned.

Call the reverseString() method recursively with the substring of the input string beginning with the second character.

To the end of the reversed substring, append the first character of the input string.

The reversed string is returned.

Conclusion

In this post, we covered various ways to reverse a text in Java using arrays. The methods used were a character array, StringBuilder, StringBuffer, a for loop, and recursion. You can use any of these ways depending on your needs and tastes. However, the first method, which uses a character array, is Java's most efficient and recommended method for reversing a string.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA