How to Reverse String in Java

Connect with

Oracle Java
There a re different way you can reverse a string by using library, iteration and recursive iteration. This is one of the important interview question which is being asked frequently specially in 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 question can be asked .

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

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);

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);

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;
}

By using Recursion

/**
 * 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);

}

By using In place

This approach is used to reverse in place , it means first convert into an 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);
}

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:
 *  - StringBuilder
 *  - StringBuffer
 *  - Iterativly
 *  - recursivly
 *  
 * @author ranjeet.jha
 *
 */
public class ReverseString {

	/**
	 * @param args
	 */
	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);

	}
}

Output of Program

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

Suggestions are welcome to improve this post.


Connect with

1 thought on “How to Reverse String in Java

  1. Pingback: Ankita

Leave a Reply

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