Wednesday, July 26, 2017

String reverse with recursion example

public class strReverse {

    public String sample;
    String rev = "";

    public String revStr(String s) {

 if (s.length() <= 1)
     return s;

 rev += s.charAt(s.length() - 1)
  + revStr(s.substring(0, s.length() - 1));
 return rev;
    }

    public static void main(String[] args) {

 strReverse strrev = new strRev();
 String after = strrev.revStr("1234");
 System.out.println(after);

    }

}