// ex_ch8_4.c
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

int main(void)
{
	int ch;
	float avg_wordlen;
	int wordchars = 0;
	int words = 0;
	bool in_word = false;
	
	// scan the input
	while ((ch = getchar()) != EOF)
	{
		if (isspace(ch) || ispunct(ch))
		{
			// a space or punctuation
			if (in_word)
				// it was in a word	
				words++;
			in_word = false;
		}
		else
		{
			in_word = true;
			wordchars++;
		}
	}
	// calculate the average word length
	avg_wordlen = (float)wordchars / words;
	// display the average word length
	printf("Average word length: %.2f.\n", avg_wordlen);
	return 0;
}
