From 7d99fb94afd75c60f8d8add3c316c275d1b3cc6b Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 16 Oct 2020 20:35:26 +0530 Subject: [PATCH] Added Reverse word in a string --- leetcode/cpp/string/151.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 leetcode/cpp/string/151.cpp diff --git a/leetcode/cpp/string/151.cpp b/leetcode/cpp/string/151.cpp new file mode 100644 index 00000000..d633b4c8 --- /dev/null +++ b/leetcode/cpp/string/151.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + string reverseWords(string s) { + string result; + int i = 0; + int n = s.length(); + + while(i= n){ + break; + } + int j = i+1; + while(j < n && s[j] != ' ')j++; + string sub = s.substr(i, j-i); + if(result.length()==0){ + result = sub; + } + else{ + result = sub + " " + resul; + } + i = j+1; + } + return result; + } +};