From 3a50af8aa78e64c6a4e45ca4b247e705e83f58fe Mon Sep 17 00:00:00 2001 From: Swaroop Phatak Date: Wed, 29 Oct 2025 23:35:43 +0530 Subject: [PATCH] Implement function to reverse words in a string --- reverseWords_swaroop-phatak.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 reverseWords_swaroop-phatak.cpp diff --git a/reverseWords_swaroop-phatak.cpp b/reverseWords_swaroop-phatak.cpp new file mode 100644 index 0000000..f28f3a4 --- /dev/null +++ b/reverseWords_swaroop-phatak.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + string reverseWords(string s) { + stringstream ss(s); + string word, res; + vector words; + while (ss >> word) words.push_back(word); + reverse(words.begin(), words.end()); + for (int i = 0; i < words.size(); i++) { + if (i) res += " "; + res += words[i]; + } + return res; + } +};