/** 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
        
        // {s contains at least 1 character}
        // return 1  + (number of chars in s[1..]
        return 1 + length(s.substring(1));
        
    }
    

    
    /** Print all "e"s in String s 
     * e.g. s is "1ete4e"  1,3,5*/
    public static void printE(String s) {
        if (s.equals("")) 
                return;
            
        // s has at least one character
        if (s.charAt(0) == 'e') {
            System.out.println(s.charAt(0));
        }
        
        // Print all 'e's in s[1..]
        printE(s.substring(1));
    }
    
    /** = print the first 'e' in s (if any)*/
    public static void printE1(String s) {
        if (s.equals("")) 
                return;
        
        // s has at least one character
        if (s.charAt(0) == 'e') {
            System.out.println(s.charAt(0));
            return;
        }   
        
        // Print the first 'e's in s[1..]
        printE1(s.substring(1));
        
    }
   
    
    /** = the number of e's in s */
    public static int numberE(String s) {
       
        
        // First character is an 'e'
        return 0;
    }
    

    
    
    /** = s but with each character duplicated
     * e.g. dup("aabc") is "aaaabbcc"*/
    public static String dup(String s) {
        if (s.length() == 0)
            return s;
        
        // {s has at least one character}
        // the answer is:
        // s[0] + s[0] + (s[1..] with each
        //                char duplicated)
        return s.charAt(0) + 
            (s.charAt(0) + dup(s.substring(1)));
    }
    
    /** s consists of a sequence of at least one integer separated
        by colons ':'.
        Return the sum of the integers.
        e.g. getSum("34:2:1) = 37.
             getSum("34") = 34.
             
             Remember: Integer.valueOf("3") = 3
        */
    public static int getSum(String s) {
         int k;  // index of first : (-1 if none)
         k= s.indexOf(':');
         if (k == -1) {
             //base case: s contains an integer
             return Integer.valueOf(s);
         }
         
         // Recursive case: s[k] is a colon
         String first= s.substring(0,k);
         String rest= s.substring(k+1);
         return Integer.valueOf(first) +
             getSum(rest);
         
        
        
    }
    
    /** = "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));  
    }
    
    /** = reverse of s 
     * e.g. rev("abc") is "cba"*/
    public static String rev(String s) {
        
        
        
        return "";
    }
}