Wednesday, 13 June 2018

Reverse String using recursion in Java



Sample Program:-


 public class SampleProgram {  
      public static void main(String[] args) {  
           String str = "abcde";  
           reverseStringRecurively(str);  
      }  
      private static void reverseStringRecurively(String str) {  
           if (str.length() == 1) {  
                System.out.print(str);  
           } else {  
                reverseStringRecurively(str.substring(1, str.length()));  
                System.out.print(str.substring(0, 1));  
           }  
      }  
 }  

Output:-


 edcba  

Enjoy Coding

No comments:

Post a Comment