//Natalie Zarate                  CSC 5                   Chapter 10, P. 589, #4
/*******************************************************************************
 * EVALUATE PHRASE
 * 
 * This program will prompt the user for a phrase within 40 characters. With 
 * this phrase, the program will find and display the word count and average 
 * word length of the phrase.
 * 
 * INPUT
 *   phrase - user inputted phrase
 * 
 * OUPUT
 *   wordCount - given phrase's word count
 *   wordLengthAVg - given phrase's average word length
 * ****************************************************************************/
#include <string> 
#include <iostream> 
#include <cctype> 
using namespace std; 

/*******************************************************************************
 * countWords
 *   Counts the number of words in a given phrase
 * _____________________________________________________________________________
 * INPUT
 *   string -  Pointer to the phrase 
 *   letter - flag; indicates whether character is a letter or space
 * 
 * OUTPUT
 *   words -  Number of words in the given phrase. 
 * ****************************************************************************/
// Prototype for countWords
int countWords(const char *string); 

/*******************************************************************************
 * avgWordL
 *   This funtion calculates the average word length of the given phrase
 * _____________________________________________________________________________
 * INPUT
 *   string - pointer to given phrase
 *   words  - phrase word count
 * OUTPUT
 *   average - average word length
 * ****************************************************************************/
// Prototype fot avgWordL
float avgWordL(const char *string, int words); 

int main()
{
	char phrase[41];    // INPUT - stores given phrase
	int wordCount;      // OUTPUT - word count of phrase
	float wordLengthAvg; // OUTPUT - average word length
	
	// Prompt user for phrase
	cout << "Enter Phrase (40 Character Limit):" << endl;
	
	// Get length of phrase
	cin.getline(phrase,sizeof(phrase));
	
	// Call countWords
	wordCount = countWords(phrase);
	
	// Display wordCount
	cout << "WORD COUNT: " << wordCount << endl;
	
	// Call avgWordL 
	wordLengthAvg = avgWordL(phrase, wordCount);
	
	// Display average owrd length
	cout << "AVERAGE WORD LENGTH: " << wordLengthAvg << endl;
	
	return 0;
}

int countWords(const char *string) 
{ 
	int words = 0; 
	bool letter = false; // INPUT - flag; dictates whether current character is a 
	                    //         letter or space
	
	//Step through array
	while (*string)
	{
		// Check if character is a string
		if (isspace(*string))
		{
			//Character is a space/ not a letter
			letter = false;
		}
		else if (!letter)
		{
			//Character is a letter
			letter = true;
			
			// Increment number of words
			words++;
		}
		
		// Move to next element
		string++;
	}
	
return words;
}

float avgWordL(const char *string, int words)
{
	int wordLength; // INPUT - length of word in given phrase
    bool letter;    // Flag; indicates whether a character is a letter or space
    float average;  // OUPUT - average word length in phrase
    
    //Step through array
    while(*string)
    {
    	// Check if character is a space
    	if(isspace(*string))
    	{
    		// Character is a space
    		letter = false;
    	}
    	else
    	{
    		letter = true;
    		
    		// Increment word length
    		wordLength++;
    	}
    	
    	string++;
    }
    
    // Coumpute average word length
    average = wordLength / words;
       
return average;


}