OK this is an easy program (since it's not too long or hard to understand) but since i want to use recursion to reverse a sentence around like "Hello!" turns into "!olleH", it makes it confusing. Here is my code:
Of course the problem is witht eh recursive function but i'm confused and i am having problems following the code since ti doesn't flip it around. I can follow the logic but i can't seem to find the problem. If anyone can give me suggestions about fixing it would be appreciated. Thank you for you time.Code:#include <iostream> #include <string> #include <vector> using namespace std; class Sentence { public: Sentence(string n); string get_text(); void reverse(); private: string phrase; }; Sentence::Sentence(string n) { phrase = n; } string Sentence::get_text() { return phrase; } void Sentence::reverse() { char first = phrase[0]; char last = phrase[phrase.length() - 1]; if (first == last) { } else { char temp = first; first = last; last = temp; string Short = phrase.substr(1,last-1); Sentence shorter_phrase(Short); shorter_phrase.reverse(); } } int main() { Sentence greeting("Hello!"); greeting.reverse(); cout<<greeting.get_text() <<"\n"; return 0; }



LinkBack URL
About LinkBacks


