Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Reverse input without array


1. Reverse input without array
This page looks at how to read input and then print it out in reverse order without an explicit array.

2. With an array
Here is the code to read in input until an end of file and then print the lines in reverse order. Here is the C code.

Note: This code has a built-in limit of 16 on the maximum number of lines in the input.

3. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.


4. Without an array
Now let us try to write this program without an array. To do so, the input and output will not be directly separated and recursion will be used. Here is the C code.

Note: This code has no built-in limit on the maximum number of lines in the input.

5. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.


6. Data flow solution
A data flow solution is largely based on the functional idea that no variable is ever assigned a value more than once.

Notice that in the above recursive program no variable is ever assigned a value more than once.

7. Notes
Why did the above work without an array?

It worked because the runtime stack was used to store the string values.

Note the following:

8. End of page