Java program to reverse a string using recursion

Java Programming Java8Java Technologies

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
raja
Published on 25-Apr-2018 11:48:48
Advertisements