-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverse-Words-in-a-String.cpp
48 lines (45 loc) · 1.11 KB
/
Reverse-Words-in-a-String.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
string reverseString(string &str){
// Vector to store words
vector<string> splitted;
string temp = "";
int start = 0, n = str.size();
// Splitting the string
for(int i=0; i<n; i++) {
// If space is found
if(str[i] == ' ') {
// If there is a word before the space
if(i > start) {
// Push the word into the vector
splitted.push_back(temp);
}
// Update the start index
start = i + 1;
temp = "";
} else {
// If there is no space
// Append the character to the temp string
temp += str[i];
}
}
// If there is a word after the last space
if(start < n) {
// Push the word into the vector
splitted.push_back(temp);
}
string res = "";
// Start from the last word
vector<string>::reverse_iterator it = splitted.rbegin();
// Append the words in reverse order
while(it != splitted.rend()) {
res += (*it);
res += ' ';
// Move to the previous word
it++;
}
// Remove the last space
res.pop_back();
// Return the reversed string
return res;
}