public class WhileDemo {
  
  /**
   * Display the squares of numbers from 1 to n.
   */ 
  public static void printSquares(int n) {
    int i = 1;
    
    while (i <= n) {
      System.out.println(i * i);
      i++;
    }
  }
  

  /**
   * Display the string s with all of the 
   * characters in reverse order.
   */ 
  public static void printRString(String s) {
    int i = s.length() - 1;
    
    while (i >= 0) {
      System.out.print(s.charAt(i));
      i--;
    }
    System.out.println();
  }


  /**
   * convert the string s with all of the 
   * characters in reverse order.
   * returns the reverse of string
   */ 
  public static String reverseString(String s) {
    String rev = "";
    int i = s.length() - 1; // start at the last char
    
    while (i >= 0) {
      rev = rev + s.charAt(i);
      i--;
    }
    return rev;
  }
  
  
  public static String reverseString2(String s) {
    String rev = "";
    int i = 0; // start at the first char
    
    while (i < s.length()) {
      rev = s.charAt(i) + rev;
      i++;
    }
    return rev;
  }
  
  /**
   * Return  true if the string is a palindrome (i.e.
   * its reverse is the same as itself. Otherwise, return false. 
   * Also return false if the string is empty ("").
   * Take into account case, and extra spaces at the
   * beginning and end of the string.
   */
  public static boolean isPalindrome(String s) {
    s = s.trim().toLowerCase();
    
    if (s.equals("")) {
      return false;
    } else {
    return s.equals(reverseString(s));
    }
  }
  
  public static boolean isPalindrome2(String s) {
    
    while ((s.charAt(0) == s.charAt(s.length() - 1))
             && s.length() > 1) {
      s = s.substring(1, s.length() - 1);
    }
    
    return s.length() <= 1;    
  }
  
  /**
   * Return the number of times that character c
   * appears in String s.
   */
  public static int countChars(String s, char c) {
    int i = 0;
    int numChars = 0;
    
    while (i < s.length()) {
      if(s.charAt(i) == c) {
        numChars++;
      }
      i++;
    }
    
    return numChars;
  }
  
  /** think about this one, and trace to see what it does! */
  public static void mystery() {
    int i = 0;
    int j = 0;
    
    while (i < 3) {
      System.out.println("i = " + i);
      while (j < 2) {
        System.out.println("j = " + j);
        j++;
      }
      j = 0;
      i++;
    }
  }   
  
}