1: // Filename: REVSTR3.CPP
2: // Purpose: Reads in a string and writes it out reversed.
4: /*
5: Currently does not work. Try as input:
6: This is the string.
7: */
9: #include <iostream>
10: #include <string>
13: void ReverseString(String s, String& r)
14: // Pre: The string "s" has been initialized.
15: // Post: "r" contains the same characters as "s"
16: // but in reverse order.
17: {
18: r = "";
19: String ch;
20: int i;
21: for (i = 0; i < s.length(); i++)
22: {
23: ch = s(i, 1);
24: r = ch + r;
25: }
26: }
29: int main()
30: {
31: cout << endl
32: << "This program reads in a string (the "
33: << "maximum length is 20 characters) and " << endl
34: << "and then prints out the string reversed." << endl
35: << endl;
36: cout << "So, enter any string of up to 20 characters: " << endl;
38: String s;
39: String r;
40: cin.getline(s, 20, '\n');
41: ReverseString(s, r);
43: cout << endl;
44: cout << "===== The string on the next line ... " << endl
45: << s << endl
46: << "===== when reversed, becomes:" << endl
47: << r << endl
48: << endl;
50: return 0;
51: }