import java.io.PrintStream;
/** A class containing word-related utilities 
 *  that are all static methods (Therefore we call them with the class name)
 *  Here we print to the console, and use the String.charAt method.
 *  Characters are primitives - and you do not need to use the equals()
 *  method on a char.  You can simply compare characters using ==
 */
public class NeatStuff2 {
  /** Displays message in JOptionPane indicating whether or not
   *  the String word is a palindrome.
   */
  public static void checkPalindrome(String word) {
    boolean isPalindrome = true;
    int i = 0;
    while (( i < (int) word.length() / 2) && isPalindrome){ /* check condition */
    /** as long as the condition is true the body of statements are executed. */
      if (word.charAt(i) != word.charAt(word.length() - (i+1))) {
         isPalindrome = false;
      }
      i++;  /** if i is not incremented we may not satisfy our condition 
             *  in some cases, this could cause the program to loop endlessly. */
    }
    if (isPalindrome) { /* if the condition is true - perform statement body */
      System.out.println("This is a palindrome!!! Smart thinking! " );
    } else {           /* otherwise perform this statement body */
      System.out.println("This is not a palindrome!!! What were you thinking? " );
    }
  } 
 /** Returns the number of times that the String containing a letter occurs in the
  * String containing a phrase.  Next lesson we will look at easier way to perform
  * this using a char primitive.
  */
  public static int countLetter(String phrase, char letter) {
    int i = 0;
    int count  = 0;
    while (i < phrase.length()) { /** while the condition is true execute this body */ 
      if (phrase.charAt(i) == letter) {
        count++;
      }
      i++;  /* remember to increment i or you will stay in the loop forever */
    }
    return count;
  }
  /** Returns the number of words in the phrase.  Assumes that all words are delineated
   *  by a single space.  
   */
  public static int countWords(String phrase) {
    int i = 0;
    int count  = 0;
    while (i < phrase.length()) {
      if (phrase.charAt(i) == (' ')) {
        count++;
      }
      i++;  /* remember to increment i or you will stay in the loop forever */
    }
    return count + 1 ; /** add 1 because there is no space before the first word */
    }
}
    
    
    
