How to reverse String in place in Java - Example

One of the common Java coding interview questions is to write a program to reverse a String in place in Java, without using additional memory. You cannot use any library method e.g. StringBuilder to solve this problem. This restriction is placed because StringBuilder and StringBuffer class defines a reverse() method which can easily reverse the given String. Since the main objective of this question is to test the programming logic of candidate, there is no point giving him the option to use the library method which can make this question trivial. Now, how do you solve this problem? Since String is backed by a character array, You can use the same in place algorithm we have used to reverse an array in place. That technique uses the two pointer approach where one pointer starts from the beginning and other pointer starts from the end. You swap elements until they meet. At that point of time, your String is already reversed.
Read more »