/*---------------------- reverse.cpp ----------------------------------- Given a string, produce another string in reverse order. For example, given "wonder" return "rednow". Note: function reverse() has many features that you might expect to find in other string processing functions, such as upto() or replace(). Note the use of a constructor to create an output string, the use of a loop to go through the input string, and subscript operators to inspect individual characters. Programmer: Jesse Thilo and Glenn Blank ---------------------------------------------------------------------*/ #include #include string reverse(string in) //returns string whose characters reverse order of string in { string out(in); //construct same size string int i=0; //subscript for definite iteration while (i < out.length()) //for each character of string in //invariant: 0 <= i < in.length() { out[out.length()-1-i] = in[i]; //insert char from in to out i= i+1; //variant: increment i by 1 } return out; //reversed string } //add replace() or upto() here? void main() { //test reverse() string test="wonder"; cout << "reverse of 'wonder' is '" << reverse("wonder") << "'\n"; cout << "Enter a string to reverse:"; cin >> test; cout << "reverse of '" << test << "' is '" << reverse(test) << "'\n"; }