Tuesday, August 16, 2011

Exercise 1-12

Exercise 1-12  Write a program that prints its input one word per line.

#include <stdio.h>

#define IN 1
#define OUT 0

int main(void)
{
  int c;
  int state;

  state = OUT;

  while (c=getchar(), c!=EOF)
  {
    switch(c)
    {
    case ' ':
    case '\t':
    case '\n':
      if (state == IN)
      {
        state = OUT;
        putchar('\n');
      }
      break;

    default:
      state = IN;
      putchar(c);
    }
  }

  if (state==IN)
    putchar ('\n');

  return 0;
}

No comments:

Post a Comment