Thursday, August 18, 2011

Exercise 1-19 (continued)

Exercise 1-19  Write a function reverse(s) that reverses the character string s.  Use it to write a program that reverses its input a line at a time.

Although this program does not satisfy the specification exactly, it does do the same job, much better than the program that does satisfy the specification exactly.

#include <stdio.h>

int reverse(void)
{
  int c = getchar();
  int d;

  if (c=='\n' || c==EOF)
    return c;

  d = reverse();
  putchar(c);
  return d;
}

int main(void)
{
  while (reverse()=='\n')
    putchar('\n');

  return 0;
}

No comments:

Post a Comment