/
Reverse Words in a String.cpp
82 lines (76 loc) · 2.21 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//============================================================================
// Reverse Words in a String
// Given an input string, reverse the string word by word.
//
// For example,
// Given s = "the sky is blue",
// return "blue is sky the".
//
// click to show clarification.
//
// Clarification:
// What constitutes a word?
// A sequence of non-space characters constitutes a word.
// Could the input string contain leading or trailing spaces?
// Yes. However, your reversed string should not contain leading or trailing
// spaces.
// How about multiple spaces between two words?
// Reduce them to a single space in the reversed string.
//============================================================================
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
#include "sstream"
#include "iterator"
using namespace std;
class Solution {
public:
void reverseWords(string & s) {
reverseWords2(s);
}
void reverseWords1(string &s) {
int i = 0;
string re;
while (i != s.length()) {
while (i != s.length() && s[i] == ' ') {
++i;
}
string temp;
while (i != s.length() && s[i] != ' ') {
temp += s[i];
++i;
}
if (temp.length() && re.length()) {
re = temp + " " + re;
} else if(temp.length()){
re = temp;
}
}
s = re;
}
void reverseWords2(string &s) {
istringstream iss(s);
ostringstream oss;
vector<string> vs;
string temp;
while(iss >> temp) vs.push_back(temp);
reverse(begin(vs), end(vs));
ostream_iterator<string> out_it (oss," ");
copy (vs.begin(), vs.end() -1, out_it);
oss << *(vs.end() -1);
// for (int i = 0; i < vs.size(); i++) {
// if (i != 0) oss << " ";
// oss << vs[i];
// }
s = oss.str();
}
};
int main(int argc, char const *argv[])
{
Solution s;
string str = "a b c ";
s.reverseWords(str);
cout << "|" << str << "|";
return 0;
}