-
Notifications
You must be signed in to change notification settings - Fork 4
/
reverse_words.cpp
48 lines (41 loc) · 1.23 KB
/
reverse_words.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
/**
* Author: Skylar Payne
* Date: January 1, 2015
* Given a sentence, reverse the words in each sentence.
* Ex: "Hello my name is Skylar. Nice to meet you." -> "Skylar is name my Hello. you meet to Nice."
**/
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
//Note: can easily extend to other types of punctuation with a dictionary of terminating punctuation.
std::string reverse_words(std::string const& sentences) {
std::stack<std::string> words;
std::stringstream reverse;
int start = 0;
for(int i = 0; i < sentences.size(); ++i) {
if(sentences[i] == ' ') {
words.push(sentences.substr(start, i - start));
start = i + 1;
}
if(sentences[i] == '.') {
words.push(sentences.substr(start, i - start));
start = ++i + 1; //this increments i past the period and following space.
reverse << (reverse.tellp() == 0 ? "" : " ");
while(!words.empty()) {
reverse << words.top() << (words.size() == 1 ? "." : " ");
words.pop();
}
}
}
return reverse.str();
}
int main(int argc, char** argv) {
if(argc != 2) {
std::cout << "Please provide the string" << std::endl;
return -1;
}
std::string sentences(argv[1]);
std::cout << reverse_words(sentences) << std::endl;
return 0;
}