Saturday, 16 December 2017

Write a java program to reverse a string or line using recursion.

public class Reverse {
public static void main(String[] args) {
String rev="Hello here is reverse using recursion";
revrec(rev);
//System.out.println(rev);
System.out.println(revrec(rev));
}

static String revrec(String rev) 
                 {
 
                 String reverse ="";
// TODO Auto-generated method stub
if(rev.length() == 1){
return rev;
}
else
{
 
reverse += rev.charAt(rev.length()-1)+revrec(rev.substring(0, rev.length()-1));
 
return reverse;
                 }
}
   

}