-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_words_in_a_string.cpp
93 lines (79 loc) · 2.3 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
83
84
85
86
87
88
89
90
91
92
93
// https://oj.leetcode.com/problems/reverse-words-in-a-string/
// Date: Nov 19, 2014
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
// Note: leetcode provides alternate solution which doesn't need stack.
// Continental Automotive expected this in the interview.
void reverseWords(string &str)
{
vector<string> vecToken;
tokenizer(str, vecToken);
stack<string> stk;
for (vector<string>::iterator it = vecToken.begin(); it != vecToken.end(); ++it)
{
//cout << "stk elem pushed:" << *it << endl;
stk.push(*it);
}
if (stk.empty())
{
str = "";
}
else
{
string outStr;
//cout << "stk elem:" << stk.top() << endl;
outStr = stk.top();
stk.pop();
while (!stk.empty())
{
outStr += " ";
outStr += stk.top();
//cout << "stk elem:" << stk.top() << endl;
stk.pop();
}
//cout << "reversed words:" << outStr << endl;
str = outStr;
}
}
private:
void tokenizer(string& str, vector<string>& vecToken, string delimiters=" ")
{
int prevPos = str.find_first_not_of(delimiters);
int curPos = str.find_first_of(delimiters, prevPos);
while ( (prevPos != str.npos) || (curPos != str.npos) )
{
vecToken.push_back(str.substr(prevPos,curPos-prevPos));
prevPos = str.find_first_not_of(delimiters, curPos);
curPos = str.find_first_of(delimiters, prevPos);
}
}
};
int main(int argc, char* argv[])
{
string str = "the sky is blue";
/*
vector<string> vecToken;
tokenizer(str, vecToken);
for (vector<string>::iterator it = vecToken.begin(); it != vecToken.end(); ++it)
{
cout << *it << endl;
}
cout << "Tokenization done" << endl;
*/
Solution sln;
sln.reverseWords(str);
cout << "reversed words in string: " << str << endl;
return 0;
}
/*
C based tokenizer:
http://www.cplusplus.com/reference/cstring/strtok/
C++ based tokenizer:
http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
*/