Source of revline2.cpp


  1: // Filename: REVLINE2.CPP
  2: // Purpose:  Reads in a line of input and then writes out
  3: //           the line with all characters in reverse order.

  5: #include <iostream>
  6: using namespace std;

  8: void ReadRestOfCharsAndWriteInReverse();

 10: int main()
 11: {
 12:     cout << endl;

 14:     cout << "This program reads a sentence and "
 15:          << "then writes it out in reverse order."     << endl;
 16:     cout << endl;

 18:     cout << "Enter a sentence on the following line: " << endl;
 19:     ReadRestOfCharsAndWriteInReverse();

 21:     cout << endl << endl;

 23:     return 0;
 24: }


 27: void ReadRestOfCharsAndWriteInReverse()
 28: // Pre:  The standard input stream is empty.
 29: // Post: All characters entered by the user, up to the
 30: //       first newline character, have been read in and
 31: //       written out in reverse order.
 32: {
 33:     char ch;

 35:     cin.get(ch);
 36:     if (ch != '\n')
 37:     {
 38:         ReadRestOfCharsAndWriteInReverse();
 39:         cout << ch;
 40:     }
 41: }