Javatpoint Logo
Javatpoint Logo

Reverse a String in Place in Java

While a String is in use, it is still feasible to reverse it. Since String in Java is immutable, it is neither possible to reverse the same String; nevertheless, by using changing StringBuilder or StringBuffer, you can limit the amount of intermediary String objects. The method we used earlier for reversing an array in place is identical to the method we will employ to reverse the String in place. To get to the middle of the String, you must move from one end of it to the other while switching characters at each end. The characters from your String are inverted at this point. To enable character switching, this method simply needs one more character in memory.

Reverse a String in Place in Java

This algorithm's time complexity is O(n/2) When I say O(n), n is indeed the length of the String.

The reverse() method to StringBuilder should be used whenever you need to reverse the String in your application. The character order of both the String in situ is reversed by this function. Additionally, the surrogate pairings are handled properly. If any surrogate pairs are present in the sequence, the reverse operation treats them as single characters.

As a result, the high-low surrogates' order never changes. Be aware that the reverse operation may produce surrogate pairs that were previously unpaired with low surrogates and high surrogates. As an illustration, reversing "uDC00uD800" results in "uD800uDC00," which is a legitimate surrogate pair.

Example of a Java program to reverse a string in place

The following is a sample Java program that resolves the issue of reversing a string in place without the need for extra memory, with the exception of one or two variables that keep track of places. We used an iterative approach to reverse String in this example.

It initially converts String into StringBuilder so that it can be altered without producing temporary String objects. Then, until it reaches the middle, it loops over StringBuilder and switches characters from both ends. At that time, without any more RAM in use, the String is inverted.

This is a prominent topic on string-based coding that is frequently asked in interviews for programming jobs in Java as well as other languages.

This is a prominent topic on string-based coding that is frequently asked in interviews for programming jobs in Java as well as other languages. One of the finest resources for performing well in Java interviews is to practice by working through the challenges provided in these coding interview classes.

This figure shows how the logic for reversing the String in place, and you can see how the characters are switched at each iteration of our loop:

Let's now watch the program in action.

File name: StringReplacing.java

Output:

The original String is : 149
Reversed String is : 941

As you can see, we are simply changing characters in StringBuilder rather than constructing new String instances. When employing this algorithm, it's typical for Java programmers to explore the entire String rather than pause at a specific point.

The error is the same as the one we discussed earlier when inverting the ArrayList in place. In a full traversal of the String, switching each character twice causes them to return to their initial positions, leaving the String unchanged.

All that is required to reverse a String using Java is the code above. The same algorithm may be used in Java to reverse any array, including String and Integer arrays. In Java, String is supported by a character array as well. As more practise, you may try utilizing Java without recursion to reverse any singly linked list. Although it's a little difficult, you can achieve it if you use logic.







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