Reverse String using recursion in java



In this core java programming tutorial we will write a program to Reverse String using recursion in java.



Hi! In this post we will reverse string using recursion.

Original String: abcde
Reversed String: edcba



Full Program/SourceCode/ Example to Reverse String using recursion in java >
/** Copyright (c), AnkitMittal www.JavaMadeSoEasy.com */
public class ReverseStringRecursionExample {
   public static void main(String...args){
          String originalString="abcde"; //String to be reversed
         
          System.out.println("Original String: "+originalString);
          System.out.print("Reversed String: ");
          reverseRecursively(originalString);
   }
  
   /*
   * reverse string recursively.
   */
   public static void reverseRecursively(String str) {
          if (str.length() == 1){
                 System.out.print(str);
          }
          else {
                 reverseRecursively(str.substring(1, str.length()));
                 System.out.print(str.substring(0, 1));
          }
   }
}
/*OUTPUT
Original String: abcde
Reversed String: edcba
*/


Previous program                                                                  Next program


So in this core java programming tutorial we wrote a program to Reverse String using recursion in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>










No comments:

Post a Comment