// Author.....: Jessie R. Hernandez
// Date.......: 02/02/2001
// Description: Counts the number of words in a given string.


// custom function 'isspace', just like in good ol' C++!

function isspace(theChar)
{
	// 'result' will indicate if the given character is a whitespace character 
	// or not
	var result = false;

	switch( theChar )
	{
		case " ":
		case "\f":
		case "\r":
		case "\n":
		case "\t":
			result = true;
			break;
	} // endswitch

	return result;
} // function isspace(theChar)

////////////////////////////////////////////////////////////////////////////////

function countWords(textString)
{
	// 'curPos' will be the current position we are in in the string 
	// 'textString'
	var curPos = 0;
	// 'gettingWord' will indicate if we are in the middle of a word (this is 
	// needed because two words might be separated by more
	// than one whitespace character)
	var gettingWord = false;
	// 'wordCount' contains the number of words in 'textString'
	var wordCount = 0;
	// to avoid having to calculate the string length on every iteration of the 
	// below loop, we will use 'textLength' to hold the length
	var textLength = textString.length;

	for(; curPos < textLength; ++curPos)
	{
		if( ( isspace( textString.charAt( curPos ) ) == true ) &&
			( gettingWord == true                            ) )
		{
			// we have gotten one more word
			++wordCount;

			// we have stopped getting a word
			gettingWord = false;
		}
		else if( isspace( textString.charAt( curPos ) ) == false )
		{
			// we are in the middle of a word
			gettingWord = true;
		} // endif
	} // endfor

	// since a whitespace character might not follow the last word, we must 
	// check to see if we were in the middle of a word
	if( gettingWord == true )
		++wordCount;

	return wordCount;
} // function countWords