/*
 * wordfreq.c
 *
 * This program reads a text file from standard input, and constructs a table
 * containing all the words occurring in it, together with the number of times
 * each word occurs.  It then prints out the words and frequencies in
 * alphabetical order.
 *
 * Written by: R. Bjork - last revised 02/03/1993
 * Modified by: J. Senning - 01/24/1998
 * Modified by: J. Senning - 01/06/2000
 * 	- Only reformating changes.
 */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "wordtable.h"

static char *readword( WORD word )  /* Read a word from standard input - return
				    address if one found, NULL if none found */
{
    int len = 0;
    int c;

    /* Advance c to first letter (if any) or EOF */
    do {
        c = getchar(); 
    } while ( c != EOF && !isalpha( c ) );

    /* Build the word */
    while ( isalpha( c ) )
    {
	if ( islower( c ) ) {
	    c = toupper( c );
	}
        word[len++] = c;
        c = getchar();
    }
    word[len] = '\0';
    return ( len > 0 ) ? word : NULL;
}

int main( void )
{
    WORD word;
    nodeptr p;
    while ( readword(word) != NULL )
    {
        p = lookup( word );
        if ( p ) {
	    p -> frequency ++;
	} else {
	    insert(word);
	}
    }
    printwords();
    return 0;
}
