/** Demo recursive methods 22 February */
public class Demo {
    
    /** = number of chars in s */
    public static int length(String s) {
        // BASE CASE
        if (s.equals(""))
            return 0;
        
        // RECURSIVE CASE
        // SOLVE THE WHOLE PROBLEM IN TERMS OF
        // A THE SAME PROBLEM ON A SMALLER SCALE
        // return 1 + number of chars in s[1..]
        return 1 + length(s.substring(1));
    }
    

    
    /** Print out all "e"s in String s */
    public static void printE(String s) {
        if (s.equals("")) {
            return;
        }
        
        // s has at least one char 
        if (s.charAt(0) == 'e') {
           System.out.print(s.charAt(0));   
        }
        
        printE(s.substring(1));
    }
    
    
    
    
    
    
    
    
    
    
    
    /** = the number of e's in s */
    public static int numberE(String s) {
        // BASE CASE
        if (s.length() == 0)
            return 0;
        
        // Recursive case. s has at least 1 character
        // Return (no e's in first char) +
        //          (no of e's in s[1..])
        return (s.charAt(0) == 'e' ? 1 : 0) +
               numberE(s.substring(1));
    }
    
    
    
    
    /** = s but with each character duplicated */
    public static String dup(String s) {
        // BASE CASE
        if (s.length() == 0)
            return "";
        
        
        // RECURSIVE CASE. s has at least 1 char
        // return s[1]+s[1] +s[1..] woth each char duplicated
        return "" + s.charAt(0) + s.charAt(0) +
               dup(s.substring(1));
    }
    
    /** = "s is a palindrome" */
    public static boolean isPal(String s) {
        // BASE CASE
        if (s.length() <= 1)
            return true;
        
        // RECURSIVE CASE: s has at least 2 chars
        // Let n be s.length() -1.
        // return value of: "s[0] = s[n] and 
        //                  s[1..n-1] is a palindrome"
        int n= s.length() -1 ;
        return s.charAt(0) == s.charAt(n) &&
               isPal(s.substring(1,n));  
    }
}