Reverse a String in Place in Java17 Mar 2025 | 4 min read 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. ![]() 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 placeThe 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. |
How many There are five different ways to create an object in Java: Java new Operator Java Class.newInstance() method Java newInstance() method of constructor Java Object.clone() method Java Object Serialization and Deserialization 1) Java new Operator This is the most popular way to create an object in Java. A new operator is...
6 min read
In the world of Java programming, there are numerous scenarios where you might need to count the distinct characters in a given string. Whether we are working on a text analysis tool, a word game, or any application that deals with text data, knowing how to...
4 min read
Java Scanner class provides Int() method for reading an integer value, Double() method for reading a double value, Long() method for reading a long value, etc. But there is no Char() method in the Scanner class to read a character in Java. In this section, we...
2 min read
Java is a versatile and widely used programming language that provides a number of features for developing complex and efficient software applications. The two main concepts in Java object-oriented programming are extensions and implementations. These two keywords play an important role in class relations and how...
5 min read
The Pig Game, also known as "Pig Dice Game" or "Pass the Pigs," is a simple and entertaining dice game that can be implemented using Java programming language. It involves rolling a pair of dice and accumulating points based on the outcome. The objective of the...
8 min read
Java is one of the rapidly growing programming languages worldwide. Most of the companies are chosen Java to build Desktop, Web, and Mobile applications. Product-based companies such as Google, Amazon, Facebook, or Microsoft have a different way to take Java interviews in comparison to the traditional...
2 min read
We have already discussed level order traversal here. In this tutorial, we will discuss how to perform the reverse level order traversal in Java. In the input, a binary tree is given to us, and our task is to print the values contained in various children...
4 min read
Java is a versatile and widely used programming language known for its robustness and readability. When it comes to creating objects with multiple attributes, the builder pattern is a popular design choice. It enhances code maintainability and readability, especially when dealing with objects with many optional...
5 min read
Rotation is one of the core problems in computer science and we want to anti clockwise rotate the elements of an array in this case. The two can be either a Left rotation where the elements are shifted leftwise and make the first element in the...
5 min read
The checkedQueue() method in the Java Collections Framework is useful for making queues type safe at the runtime and therefore it is a very effective and important utility and it is for this reason that the checkedQueue() method is in the Collections class and can...
14 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India