Using C++ create a function called reverse that accepts a string and returns the string in reverse. Must use the std::string and the iterator.
#include <iostream>
#include <string>
using namespace std;
string reverse(string str){
string output;
/*creating an iterator (it) of type string
* str.end() will point to null char of string
* hence str.end()-1 will give last char of string
*
* */
for ( std::string::iterator it=str.end()-1; it>=str.begin(); it--){
output=output+ *it;
}
return output;
}
/* optional tester code*/
int main ()
{
cout<<reverse("TEST STRING");
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.