Java Program to Reverse a String using Recursion - The Coding Shala

Home >> Java Programs >> Reverse a String using Recursion

 In this post, we will learn how to Reverse a String in Java using Recursion.

Java Program to Reverse a String using Recursion

Given a string S as input. You have to reverse the given string using Recursion.

Example 1:
Input: Akshay
Output: yahskA

Java Program: 

// Java program to Reverse a String using Recursion

public class Main {
	
	public static String reverseIt(String str) {
		if(str.length() == 0) {
			return "";
		}
		return reverseIt(str.substring(1)) + str.charAt(0);
	}
	
	public static void main(String[] args) { 
		
		String str = "Akshay Saini";
		String reverseStr = reverseIt(str);
		
		System.out.println("Reverse String is: " + reverseStr);
	}
}


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Find Second Smallest Element in the Array - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Graph Representation using Adjacency Matrix - The Coding Shala

Java Method Overloading - The Coding Shala

Time Complexity, Space Complexity, Asymptotic Notations - The Coding Shala