Tuesday, June 9, 2015

Q.4 - Program to implement string reversal in java recursively

//Program to implement string reversal in java recursively

public class ReverseString{
   public static int i = 0;
   public static void main(String args[]){
      System.out.println(ReverseString.revStr("repeed gniggid tuo ti deelB"));
   }

   public static String revStr(String str){
      char curr = ' ';
      String revStr = "";
      if(str.length() == 1)
                    return str;
              curr = str.charAt(0);
      revStr = revStr(str.substring(1,str.length()));
      revStr += curr;

      return revStr;
   }
}

No comments:

Post a Comment