/*
 * $Smake: cc -O -o %F %f
 *
 * wordcount.c
 *
 * This program copies a file from standard input to standard output.  Each
 * word from the original file appears on a separate line in the output file.
 * A count of the number of words processed is printed at the end.
 *
 * Written by: R. Bjork - last revised 02/03/1993
 * Modified by: J. Senning - 01/24/1998
 * Modified by: J. Senning - 01/06/2000
 * 	- Changed declaration of c in getword() from "char" to "int".
 */

#include <stdio.h>
#include <ctype.h>

#define WORDLEN 20    /* Max length of a word - not including trailing null */

/*
 * Read a string of alphanumeric characters (a word) from standard input and 
 * return in w.  A null string is returned if at end of file.
 */
void getword( char w[] )
{
    int c;		/* lookahead character */
    int i;		/* position in word */

    /* Skip separators */

    do {
	c = getchar();
    } while ( !isalpha( c ) && !isdigit( c ) && ( c != EOF ) );

    /* Form the word */

    i = 0;
    while ( isalpha( c ) || isdigit( c ) )
    {
        w[i++] = c;
	c = getchar();
    }

    /* Put back the lookahead character for next time */
    if ( c != EOF ) ungetc( c, stdin );

    /* Terminate word with a null */
    w[i] = '\0';
}

int main( void )
{
    char word[WORDLEN+1];	/* one word-allow extra space for final null */
    int  wordcount = 0;		/* count of number of words seen */
    while ( 1 )
    {
        getword( word );
	if ( word[0] == '\0' ) break;	/* End of file seen */
	printf( "%s\n", word );
        wordcount++;
    }
    printf( "------------------\nThere were %d words\n", wordcount );
    return 0;
}
