Tuesday, August 16, 2011

Exercise 1-9

Exercise 1-9  Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

#include <stdio.h>

int main(void)
{
  int c;
  int space = 0;

  while (c=getchar(), c!=EOF)
  {
    if (c==' ')
    {
      if (!space)
        putchar(c);

      space = 1;
    }
    else
    {
      space = 0;
      putchar(c);
    }
  }

  return 0;
}

No comments:

Post a Comment