/* Program File: wds.c
Other Source Files: chrutil.c
Header Files: tfdef.h, chrutil.h
This program reads standard input characters and prints each word on a
separate line. It also counts the number of lines, words, and characters.
All characters are counted including the newline and other control
characters, if any.
*/
#include <stdio.h>
#include "tfdef.h"
#include "chrutil.h"
#define INTERACT
/*
*/
main()
{ signed char ch;
int inword, /* flag indicating when in a word */
lns, wds, chrs; /* Counters for lines, words, chars. */
#ifdef INTERACT
printf("***Line, Word, Character Count Program***\n\n");
printf("Type characters, EOF to quit\n");
#endif
lns = wds = chrs = 0; /* initialize counters to 0 */
inword = FALSE; /* before beginning we are not in a word */
while ((ch = getchar()) != EOF) /* while there are more characters */
{ chrs = chrs + 1; /* count characters */
if (ch == '\n') /* if newline char */
lns = lns + 1; /* count lines */
/* if not in a word and not a delimiter */
/* then this must be the beginning of a word */
if (!inword && !delimitp(ch)) /* if not in word and not delim. */
{ inword = TRUE; /* remember we are in a word */
wds = wds + 1; /* count words */
}
/* otherwise if in a word, but found a delimiter */
/* then we just went beyond the end of the word */
else if (inword && delimitp(ch)) /* if in word and a delimiter*/
{ inword = FALSE; /* we are no longer in a word*/
putchar('\n'); /* end word with a newline */
}
if (inword) /* if in a word */
putchar(ch); /* print the character */
}
/* print the results */
printf("Lines = %d, Words = %d, Characters = %d\n",
lns, wds, chrs);
}