Answer to Question #43945 in C++ for joney

Question #43945
Write a function that takes two string as parameters. The function should make the second

string contain the reverse of the first string. For example, if the first string contains "two legs

bad ", then the function should set the second string to contain "dab sgel owt". Write a main

function that reads a sentence from the screen pass it to the function and prints its reverse.

Sample usage of your program:

Enter a sentence : two legs bad

The reverse is: dab sgel owt
Expert's answer
#include <iostream>
std::string reverseString(std::string palindrome) {
std::string reversed = "";
for (std::string::reverse_iterator rit = palindrome.rbegin(); rit != palindrome.rend(); ++rit) {
reversed += *rit;
return reversed;
int main() {
std::string toReverse;
std::cout << "Please enter a string" << std::endl;
std::cin >> toReverse;
std::cout << reverseString(toReverse) << std::endl;
return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog