#include #include #include #include using namespace std; string reverseWords(string str) { stringstream ss(str); vector words; string word; //put the words that are being read into the vector we constructed while (ss >> word) { words.push_back(word); } // Put the words in the vector in reverse ordeer string reversed; for (int i = words.size() - 1; i >= 0; --i) { reversed += words[i]; //put spaces betwewen words if there are still words left in the vector if (i > 0) { reversed += " "; } } return reversed; } int main() { string sentence; cout << "Please enter the sentence you want to reverse: " << endl; getline (cin, sentence); cout << "Reversed sentence is: " << reverseWords(sentence) << endl; return 0; }