/* AnalyzePhrase.java by Craig Persiko
   CS 111A Programming Lab 5 solution
   Count Words and display longest word
*/

import java.util.Scanner;

public class AnalyzePhrase
{
  public static void main(String[] args)
  {
    String phrase;
    int numWords;

    phrase = inputPhrase();

    numWords = analyzePhrase(phrase);

    System.out.println("Your phrase has " + numWords + " words in it.");
    System.out.println();
  }

  /**
    inputPhrase method
    Ask the user to input a phrase, and make sure it is at least 5 characters long
    @return the phrase
  */
  public static String inputPhrase()
  {
    Scanner keyboard = new Scanner(System.in);
    String phrase;

    do{
      System.out.println("Please enter a phrase all on one line, ");
      System.out.println("with just one space separating each word, ");
      System.out.println("and no spaces at the beginning or end of the phrase.");
      phrase = keyboard.nextLine();
      if (phrase.length() < 5)
        System.out.println("Your phrase must contain at least 5 characters.");
    }while (phrase.length() < 5);

    return phrase;    
  }

  /**
     analyzePhrase method
     Count the number of words in a given string, and determine its longest word.
     Assume the string is well formed and doesn't have leading 
     or trailing spaces, or multiple spaces in a row, and number of words is returned.
     Longest word is output on the console.
     @param str string to analyze
     @return number of words in str
  */
  public static int analyzePhrase(String str)
  {
     int count = 1, wordStart = 0;
     int wordLength, lengthOfLongestWord = 0, startOfLongestWord = 0;
     String longestWord;

     for (int i = 0; i < str.length(); i++)
     {
        char ch = str.charAt(i);
        if (ch == ' ')
        {
           wordLength = i - wordStart;
           if (wordLength > lengthOfLongestWord)
           {
             lengthOfLongestWord = wordLength;
             startOfLongestWord = wordStart;
           }
           count++;
           wordStart = i+1;
        }
     }
     // check the final word to see if it's the longest:
     wordLength = str.length() - wordStart;
     if (wordLength > lengthOfLongestWord)
     {
       lengthOfLongestWord = wordLength;
       startOfLongestWord = wordStart;
     }

     longestWord = str.substring(startOfLongestWord, startOfLongestWord + lengthOfLongestWord);
     System.out.println("The longest word in your phrase is \"" + longestWord + 
                        "\" with " + lengthOfLongestWord + " characters.");

     return count;
  }
}

/* Sample Output:

[cpersiko@fog cs111a]$ java AnalyzePhrase
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
This is my phrase
The longest word in your phrase is "phrase" with 6 characters.
Your phrase has 4 words in it.

[cpersiko@fog cs111a]$ java AnalyzePhrase
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
Hi
Your phrase must contain at least 5 characters.
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
Hello
The longest word in your phrase is "Hello" with 5 characters.
Your phrase has 1 words in it.

[cpersiko@fog cs111a]$ java AnalyzePhrase
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
Now I will try with a very long phrase that contains many words
The longest word in your phrase is "contains" with 8 characters.
Your phrase has 13 words in it.

[cpersiko@fog cs111a]$ java AnalyzePhrase
Please enter a phrase all on one line, 
with just one space separating each word, 
and no spaces at the beginning or end of the phrase.
Programming is cool
The longest word in your phrase is "Programming" with 11 characters.
Your phrase has 3 words in it.
[cpersiko@fog cs111a]$ 

*/


syntax highlighted by Code2HTML, v. 0.9