Print Reverse Lines

Code Id 9
Date Updated 3/7/2010
Title Print reverse lines  
Description
Write a program that reads a series of lines and prints the lines in reverse order: the last line, then the next-to-last line, etc.

You may assume that there are at most 1000 lines. You may assume that each line has at most 1000 characters.
You may assume that the last line of input ends with a newline.

For example, if the user types
   This
   is
   a test.
then your program will print
   a test.
   is
   This
in the order shown. 
                                  
Codes Snippet
#include 

char x[1001000];
int xlen = 0;

int y[1001];
int ylen = 0;

int main(void)
{
  char c;
  int i;
  int j;

  y[ylen++] = 0;

  while (scanf("%c",&c) == 1)
    if (xlen < 1001000) {
      x[xlen++] = c;
      if (c == 'n')
        if (ylen < 1001)
          y[ylen++] = xlen;
    }

  for (i = ylen - 1;i > 0;--i)
    for (j = y[i - 1];j < y[i];++j)
      putchar(x[j]);

  return 0;
}

Comments are closed.