How to Reverse String in Java

Connect with

How to Reverse String in JavaLearn How to Reverse String in Java, using the library, iteration, and recursive iteration, using StringBuffer, StringBuilder class, using for loop, using recursive appproach, using in place.

1. Overview of Reverse String in Java

There are different ways you can reverse a string by using the library, iteration, and recursive iteration. This is one of the important interview questions which is being asked frequently especially in the initial career. The answer is simple but if you reply one way perhaps iteration way, you are being asked to reply in another way perhaps recursive approach. There are so many string-based questions that can be asked.

2. Different Ways of Reversing String in Java

Let us demonstrate the example to reverse the string by following ways:

  • by using StringBuffer java class ( JDK library class)
  • by using StringBuilder java class ( JDK library class)
  • by using Iterative approach with for loop
  • by using recursive approach i.e. recursively

3. By Using StringBuffer Java class

// reverse string using StringBuffer in java
StringBuffer stringBuffer = new StringBuffer("ranjeet");
String reverseStr = stringBuffer.reverse().toString();
System.out.println("Output: " + reverseStr);

4. By Using StringBuilder Java class

// reverse string using StringBuffer in java
StringBuilder sb = new StringBuilder("ranjeet");
String reverseStr = sb.reverse().toString();
System.out.println("Output: " + reverseStr);

5. By Using Iterative for Loop

/**
 * This is a java method to reverse the string by using for loop , iterative approach.
 * 
 * @param inputStr
 * @return
 */
public static String reverseIterativly(String inputStr) {
	
	if (inputStr == null || inputStr.isEmpty()) {
		return inputStr;
	}
	
	String reverseStr = "";
	for (int i = inputStr.length() - 1; i >= 0; i--) {
		reverseStr = reverseStr + inputStr.charAt(i);
	}

	return reverseStr;
}

6. By using Recursion

You can reverse string using resusive approch, you can find following example code for recusion approach of reversing string.

/**
 * This is a java method to reverse the string by recursive approach.
 * 
 * @param inputString
 * @return
 */
public static String reverseByRecursively(String inputString) {

	// this if loop handle base case ,for one char string and empty string
	if (inputString.length() < 2) {
		return inputString;
	}

	return reverseByRecursively(inputString.substring(1)) + inputString.charAt(0);

}

7. By using In place

In this section, you learn in place value approach which is used to reverse in place, it means first convert into a Character array and swap the value from first to last and so on by temp Character.

/**
 * This Java method to reverse a String in place.
 * 
 * @param str
 * @return
 */
public static String reverseInPlace(String str) {
	if (str == null || str.isEmpty()) {
		return str;
	}
	char[] charArray = str.toCharArray();
	int i = 0;
	int j = charArray.length - 1;
	while (i < j) {
		char temp = charArray[i]; 
		charArray[i] = charArray[j]; 
		charArray[j] = temp;
		i++;
		j--;
	}
	return new String(charArray);
}

8. Complete source Code Example

In the above I have mentioned only method , the following are the complete example.

package com.mysoftkey.dsa;

/**
 * This java class is used to demonstrate the reversing a string by using different ways:
 *  - using StringBuilder to reverse string in java
 *  - using StringBuffer to reverse string in java
 *  - using iteratively to reverse string in java
 *  - using recursively to reverse a string in java
 */
public class ReverseString {

	public static void main(String[] args) {

		// original string
		String originalString = "ranjeet";
		System.out.println("Original String: " + originalString);
		String reverseStr = null;
		
		// reverse string using StringBuffer in java
		StringBuffer stringBuffer = new StringBuffer(originalString);
		reverseStr = stringBuffer.reverse().toString();
		System.out.println("Reverse String using StringBuffer: " + reverseStr);

		// reverse string using StringBuffer in java
		StringBuilder sb = new StringBuilder(originalString);
		reverseStr = sb.reverse().toString();
		System.out.println("Reverse String using StringBuilder: " + reverseStr);

		// iterative method to reverse String in Java
		reverseStr = reverseIterativly(originalString);
		System.out.println("Reverse String using Iteration: " + reverseStr);

		// recursive method to reverse String in Java
		reverseStr = reverseByRecursively(originalString);
		System.out.println("Reverse String using Recursion: " + reverseStr);
	}
	
	/**
	 * This is a java method to reverse the string by using for loop , iterative approach.
	 * 
	 * @param inputStr
	 * @return
	 */
	public static String reverseIterativly(String inputStr) {
		
		if (inputStr == null || inputStr.isEmpty()) {
			return inputStr;
		}
		
		String reverseStr = "";
		for (int i = inputStr.length() - 1; i >= 0; i--) {
			reverseStr = reverseStr + inputStr.charAt(i);
		}

		return reverseStr;
	}

	/**
	 * This is a java method to reverse the string by recursive approach.
	 * 
	 * @param inputString
	 * @return
	 */
	public static String reverseByRecursively(String inputString) {

		// this if loop handle base case ,for one char string and empty string
		if (inputString.length() < 2) {
			return inputString;
		}

		return reverseByRecursively(inputString.substring(1)) + inputString.charAt(0);

	}
}

9. Output of Program for reverse string

you can find output of string for reversing string in java.

Original String: ranjeet
Reverse String using StringBuffer: teejnar
Reverse String using StringBuilder: teejnar
Reverse String using Iteration: teejnar
Reverse String using Recursion: teejnar

10. Reference

I hope you enjoyed this post and learn about how to reverse string in Java, and you can visit data structure tutorial for more details.

Visit Wiki page.
Suggestions or comments are welcome to improve this post. cheers


Connect with

1 thought on “How to Reverse String in Java”

  1. Pingback: Ankita

Leave a Comment

Your email address will not be published. Required fields are marked *