/** Demo recursion */
public class D {
    
    /** = the length of s. The method cannot use s.length() */
    public static int len(String s) {
        if (s.equals("")) {  // BASE CASE
            return 0;
        }
        
        // {s has at least one character }  // RECURSIVE CASE
        // return 1 + (length of s[1..])
        return 1 +  len(s.substring(1));
    }
    
    
    /** =  number of 'e's in s */
    public static int noE(String s) {
        one: if (s.equals("")) {  // BASE CASE
            two: return 0;
        }
        // { s has at least one char }
        three: if (s.charAt(0) == 'e') {
            four: return 1 +  noE(s.substring(1));
        }
        
        // { the first char is not 'e' }
        five: return noE(s.substring(1));
    }
    
    /** = s with blanks removed */
    public static String remBlank(String s) {
        if (s.length() == 0) {
            return s;
        }
        // { s has at least one char }
        if (s.charAt(0) == ' ') {
            return remBlank(s.substring(1));
        }
        
        // { the first char of s is not a blank }
        // return the first char + (rest of s but with blanks removed)
        return s.charAt(0) + remBlank(s.substring(1));
        
    }
    
    /** = n!.  Precondition: n >= 0.
      Def: 0! = 1.
      n!=  n*(n-1)*(n-2)*...*2*1 (for n > 0).
      6! = 6*5*4*3*2*1
      
      or     0! = 1
      n! = n * (n-1)!  (for n > 0) */
    public static int fact(int n) {
        if (n <= 1) {   // Base case
            return 1;
        }
        // { n > 1 }
        return n * fact(n-1); //Recursive case
        
    }
    
    //noon
    //ablewasIereIsawelba
    // A man, a plan, a canal, panama!
    // amanaplanacanalpanama!
    
    /** = "s is a palindrome" (reads same backward and forward) */
    public static boolean isPal(String s) {
        if (s.length() <= 1) {
            return true;   
        }
        // { s contains at least 2 characters }
        int k= s.length()-1;
        //return first and last chars are the same AND
        //       s[1..k-1] is a palindrome;
        return s.charAt(0) == s.charAt(k) &&
            isPal(s.substring(1,k));
    }
    
    
}