-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseEachWord.cpp
37 lines (29 loc) · 991 Bytes
/
reverseEachWord.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
void Solution::reverseWords(string &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
string output;
int i = 0;
bool firstword = true;
while (i < A.length()){
while(isspace(A[i])){ i++; }
if (i == A.length()) break;
string word;
while (i < A.length()){
if (isspace(A[i])){
i++;
break;
}
word.push_back(A[i]);
i++;
}
//word.push_back('\0');
reverse(word.begin(), word.end());
if (!firstword) output += ' ';
output += word;
firstword = false;
}
reverse(output.begin(), output.end());
A = output;
}