java

How to Reverse a String in Java Using Recursion

Recursion is the process of repeating something in a similar way. In programming languages, if a program makes it possible to call a function within the function itself, then this is called a recursive call. You can reverse a string using a recursive function, as shown in the following program.
 

Program to reverse a tring in Java using recursion :
public class StrReverse {
  
   public static String reverseString(String str){
   
      if(str.isEmpty()){
         return str;
      } else {
         return reverseString(str.substring(1))+str.charAt(0);
      }
   }
  
   public static void main(String[] args) {

      String res = StrReverse.reverseString("StackHowTo");
      System.out.println("The reversed String : "+res);
     
   }
  
}

Output:

The reversed String : oTwoHkcatS
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *