Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ function that prints the reverse of an input string #include <iostre

ID: 3662524 • Letter: W

Question

Write a C++ function that prints the reverse of an input string

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

void reverseString(string in_str);

int main()
{
   string str1 = "Hello World!";
   string str2 = "racecar"
   string str3 = "2106AD";
   string str4 = "a man a plan a canal-panama";
   string str5 = " ";
  
   reverseString(str1);
   cout << endl;
   reverseString(str2);
   cout << endl;
   reverseString(str3);
   cout << endl;
   reverseString(str4);
   cout << endl;
   reverseString(str5);

   return 0;

}

void reverseString(string in_str)
{
   // Your code goes here


}

Explanation / Answer

Complte program code:

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
void reverseString(string in_str);
int main()
{
string str1 = "Hello World!";
string str2 = "racecar"
string str3 = "2106AD";
string str4 = "a man a plan a canal-panama";
string str5 = " ";
  
reverseString(str1);
cout << endl;
reverseString(str2);
cout << endl;
reverseString(str3);
cout << endl;
reverseString(str4);
cout << endl;
reverseString(str5);
return 0;
}
void reverseString(string in_str)
{
if (in_str.length() == 1) {
return in_str;
}else{
return reverseString(in_str.substr(1,in_str.length())) + in_str.at(0);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote