Write the word count program and test it.


/* word count program */
/* word defined by letters surrounded by whitespace. */
#include "stdio.h"

#define TRUE 1
#define FALSE 0

typedef signed char bool; 

int main(int argc, char *argv[])
  int character = ' ';
  int character_count = 0;
  int word_count = 0;
  int line_count = 0;
  bool in_word = FALSE;
  bool running = TRUE;

  printf("Type a few sentences. EOF (ctrl+d) to submit and quit.\n\n");

    character = getchar();

    if(character == '\n' || character == EOF)

    if(character == '\n' || character == ' ' || character == '\t' || character == EOF)
        in_word = FALSE;
      in_word = TRUE;
    /* We will test for EOF to quit here.*/
    if(character == EOF)
      running = FALSE;

  printf("lines:%i, words:%i, characters:%i\n", line_count, word_count, character_count);
  return 0;


Type a few sentences. EOF (ctrl+d) to submit and quit.

Hello, world!

u4e.us is cool!
lines:3, words:5, characters:31


The book asks what kinds of input are most likely to uncover bugs if there are any. I can assume it means newlines, EOF, backspaces, tabs, etc. Keep in mind the book was written with live terminal input feedback. Our current terminal emulators do not function that way by default so don't go nuts trying to figure out why the book talks about output that differs from your own. Just do your best and work through the problems thinking about what they were TRYING to show you.