I need to write a program that implements the reverse function using a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two.
I have written this code below. I have two problems. 1, I am getting a fatal error on my Visual C++ program (I am uninstalling and reinstalling now) and it is not compiling anything. I can not check to see if I have errors in this program nor can I see if this correctly answers everything I need to do. 2. I am not really fully understanding what I am supposed to be doing with this program. Did I answer everything with this code? Thanks for your help!!
#include <iostream>
#include <string>
using namespace std;
class Sentence
{
public:
Sentence(string aPhrase);
string reverse();
private:
string phrase;
};
Sentence::Sentence(string aPhrase)
{
phrase = aPhrase;
}
string Sentence::reverse()
{
string r;
for (int i = 0; i < phrase.length(); i++)
r = phrase.substr(i, 1) + r;
phrase = r;
return phrase;}
int main()
{
Sentence greeting("Hello!");
cout << greeting.reverse() << "\n";
return 0;
}