public class Recurse {

    public static void main (String[] args) {
	printString ("hello");
	printReverse ("hello");
	String backwards = reverseString ("Allen Downey");
	System.out.println (backwards);
    }

    public static void printString (String s) {
	if (length(s) == 0) return;
	System.out.println (first(s));
	printString (rest(s));
    }

    public static void printReverse (String s) {
	if (length(s) == 0) return;
	printReverse (rest(s));
	System.out.println (first(s));
    }

    public static String reverseString (String s) {
	if (length(s) == 0) return "";
	return reverseString (rest(s)) + first (s);
    }

    // first: returns the first character of the given String
    public static char first (String s) {
	return s.charAt (0);
    }

    // last: returns a new String that contains all but the
    // first letter of the given String
    public static String rest (String s) {
	return s.substring (1, s.length());
    }

    // length: returns the length of the given String
    public static int length (String s) {
	return s.length();
    }
}
