Description
Write a java program to reverse a given string using recursion without using any predefined function.This program checks the understanding of recursion in programming language and also checks the programming sense.Here, the method reverseString() is called recursively to revrse a given string.Following is the complete program.
package com.devglan; public class StringReversal { String reversedString = ""; public String reverseString(String str){ if(str.length() == 1){ return str; } else { reversedString += str.charAt(str.length()-1) +reverseString(str.substring(0,str.length()-1)); return reversedString; } } public static void main(String a[]){ StringReversal reversal = new StringReversal(); System.out.println("Reversed string - " + reversal.reverseString("planet")); } }