Skip to content

Commit 7d99fb9

Browse files
author
Your Name
committed
Added Reverse word in a string
1 parent c8a98ac commit 7d99fb9

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

leetcode/cpp/string/151.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
string reverseWords(string s) {
4+
string result;
5+
int i = 0;
6+
int n = s.length();
7+
8+
while(i<n){
9+
while(i < n && s[i] == ' ')i++;
10+
if(i >= n){
11+
break;
12+
}
13+
int j = i+1;
14+
while(j < n && s[j] != ' ')j++;
15+
string sub = s.substr(i, j-i);
16+
if(result.length()==0){
17+
result = sub;
18+
}
19+
else{
20+
result = sub + " " + resul;
21+
}
22+
i = j+1;
23+
}
24+
return result;
25+
}
26+
};

0 commit comments

Comments
 (0)