Java API, recursive & iterative algorithm to reverse string (example)

  • Given a String in java,  reverse the String using standard java classes & recursive/iterative algorithm.
    • e.g. reverse(“Lived”) = “deviL”
    • reverse(“Star”) = “ratS”
  • We will reverse string using following methods.
    • Reverse string using StringBuilder class.
    • Reverse String using StringBuffer class.
    • Java program to reverse String using iterative algorithm.
    • Java program to reverse String using recursive algorithm.

Method 1: Reverse String using StringBuilder class

public static String reverseUsingStringBuilder(String inputString) {
        //inputString = "Lived"
	StringBuilder builder = new StringBuilder(inputString);
        //Output will be: deviL
	return builder.reverse().toString();
}

Method 2: Reverse String using StringBuffer class

public static String reverseUsingStringBuffer(String inputString) {
        //inputString = "Lived"
	StringBuffer builder = new StringBuffer(inputString);
        //Output will be: deviL
	return builder.reverse().toString();
}

Method 3: Java program to reverse string – iterative algorithm

//inputString = "Lived"
public static String iterative(String inputString) {
	if (null == inputString || inputString.length() == 0)
		return "";

	int length = inputString.length();
	char[] chInputArray = inputString.toCharArray();
	for (int index = 0; index < length / 2; index++) {
		char firstHalf = chInputArray[index];
		char secondHalf = chInputArray[length - index - 1];

		//swap
		chInputArray[index] = secondHalf;
		chInputArray[length - index - 1] = firstHalf;
	}
	return new String(chInputArray);
}//Reverse string of "Lived" is "deviL"

Method 4: Java program to reverse string – recursive algorithm

//inputString = "Lived"
public static String recursive(String inputString) {

	if (null == inputString || inputString.length() == 0)
		return "";
	return recursive(inputString.substring(1)) + inputString.charAt(0);
}//Reverse string of "Lived" is "deviL"

Logical flow to reverse string is shown Fig 1.

reverse string recursive algorithm
Fig 1: Reverse String using recursive algorithm: recursive(“Lived”) = deviL.

Complete Program to reverse a string in java:

package org.learn;

import java.util.Scanner;

public class StringReverse {

	public static String reverseUsingStringBuilder(String inputString) {
		StringBuilder builder = new StringBuilder(inputString);
		return builder.reverse().toString();
	}

	public static String reverseUsingStringBuffer(String inputString) {
		StringBuffer builder = new StringBuffer(inputString);
		return builder.reverse().toString();
	}

	public static String iterative(String inputString) {
		
		if (null == inputString || inputString.length() == 0)
			return "";

		int length = inputString.length();
		char[] chInputArray = inputString.toCharArray();
		for (int index = 0; index < length / 2; index++) {
			
			char firstHalf = chInputArray[index];
			char secondHalf = chInputArray[length - index - 1];

			// swap
			chInputArray[index] = secondHalf;
			chInputArray[length - index - 1] = firstHalf;
		}
		return new String(chInputArray);
	}

	public static String recursive(String inputString) {

		if (null == inputString || inputString.length() == 0)
			return "";
		return recursive(inputString.substring(1)) + inputString.charAt(0);
	}

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			System.out.printf("1. Enter input string : ");
			String input = scanner.next();

			String reverse = reverseUsingStringBuilder(input);
			System.out.printf("2. reverseUsingStringBuilder(\"%s\"): %s", input, reverse);

			reverse = reverseUsingStringBuffer(input);
			System.out.printf("\n3. reverseUsingStringBuffer(\"%s\"): %s", input, reverse);

			reverse = iterative(input);
			System.out.printf("\n4. iterative(\"%s\"): %s", input, reverse);

			reverse = recursive(input);
			System.out.printf("\n5. recursive(\"%s\"): %s", input, reverse);
		}
	}
}

Program output to reverse string in java:

1. Enter input string : Lived
2. reverseUsingStringBuilder("Lived"): deviL
3. reverseUsingStringBuffer("Lived"): deviL
4. iterative("Lived"): deviL
5. recursive("Lived"): deviL

1. Enter input string : Star
2. reverseUsingStringBuilder("Star"): ratS
3. reverseUsingStringBuffer("Star"): ratS
4. iterative("Star"): ratS
5. recursive("Star"): ratS
Scroll to Top