1: // Filename: REVLINE3.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() // Note: VVVVVVVVVVVVVVVVVVVV 28: // Pre: The standard input stream contains **at least one character**. 29: // Post: All characters entered by the user, up to the 30: // first newline character, have been read in and 31: // writen out in reverse order. 32: { 33: char ch; 35: cin.get(ch); 36: if (cin.peek() == '\n') // Note use of cin.peek() 37: cout << ch; // to "look ahead" one character. 38: else 39: { 40: ReadRestOfCharsAndWriteInReverse(); 41: cout << ch; 42: } 43: }