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

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Introduction to Kotlin Programming Language for Backend Development - The Coding Shala