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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.