1: // Filename: REVLINE4.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: // writen out in reverse order. 32: { 33: char ch; 35: if (cin.peek() == '\n') // Note use of cin.peek() 36: ; // to "look ahead" one character. 37: else 38: { 39: cin.get(ch); 40: ReadRestOfCharsAndWriteInReverse(); 41: cout << ch; 42: } 43: }