Java program to reverse a string using recursion


Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. You can reverse a string using the recursive function as shown in the following program.

Example

Live Demo

public class StringReverse {
   public String reverseString(String str){
   
      if(str.isEmpty()){
         return str;
      } else {
         return reverseString(str.substring(1))+str.charAt(0);
      }
   }
   public static void main(String[] args) {
      StringReverse obj = new StringReverse();
      String result = obj.reverseString("Tutorialspoint");
      System.out.println(result);
   }
}

Output

tniopslairotuT

Updated on: 13-Mar-2020

788 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements