//ReverseLine.java
//Reads in a line of input and then writes out
//the line with all characters in reverse order.

import java.io.IOException;

public class ReverseLine
{
    public static void main(String[] args) throws IOException
    {
        System.out.println
        (
            "\nThis program reads a sentence and "
            + "then writes it out in reverse order."
        );

        System.out.println
        (
            "Enter a sentence on the following line "
            + "and press Enter:"
        );
        readRestOfCharsAndWriteInReverse();
        //readRestOfCharsAndWriteInReverse2();
        System.out.println();
    }

    /**
        Read a string of characters from an input line and then write
        those characters out in reverse.
        <p>Pre:<p>The standard input stream is empty.
        <p>Post: All characters entered by the user, up to the first newline
        character, have been read in and written out in reverse order.
    */
    public static void readRestOfCharsAndWriteInReverse() throws IOException
    {
        char ch = (char)System.in.read();
        if (ch == '\n')
            ; //Stop. That is, do nothing, since you're finished.
        else
        {
            readRestOfCharsAndWriteInReverse();
            System.out.print(ch);
        }
    }

    /**
        Read a string of characters from an input line and then write
        those characters out in reverse.
        <p>Pre:<p>The standard input stream is empty.
        <p>Post: All characters entered by the user, up to the first newline
        character, have been read in and written out in reverse order.
    */
    public static void readRestOfCharsAndWriteInReverse2() throws IOException
    {
        char ch = (char)System.in.read();
        if (ch != '\n')
        {
            readRestOfCharsAndWriteInReverse2();
            System.out.print(ch);
        }
    }
}

