-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
executable file
·83 lines (48 loc) · 1.79 KB
/
util.cpp
File metadata and controls
executable file
·83 lines (48 loc) · 1.79 KB
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
#include <sstream>
#include <cctype>
#include <algorithm>
#include <string>
#include <cctype>
#include "util.h"
using namespace std;
std::string convToLower(std::string src)
{
std::transform(src.begin(), src.end(), src.begin(), ::tolower);
return src;
}
/** Complete the code to convert a string containing a rawWord
to a set of words based on the criteria given in the assignment **/
std::set<std::string> parseStringToWords(string rawWord) {
string lower_case_raw_word = convToLower(rawWord); //Converts to lower case
string temp;
stringstream ss;
ss << lower_case_raw_word;
set<string> key_words; //Dynamically allocated because needs to exist outside of function scope
while (ss >> temp) {
if (temp.size() > 1) {
for (unsigned int i = 0; i < temp.size(); i++) {
if (ispunct(temp[i])) { //Checks for punctuation
string sub_str;
sub_str = temp.substr(0, i); //Creates a substring of the keyword before the punctuation
if (sub_str.size() > 1) {
key_words.insert(sub_str); //Puts it into the set
}
temp.erase(0, i+1);
i = -1; //Starts from beginning of new curtailed string
}
}
if (temp.size() > 1) {
key_words.insert(temp); //Puts the word into the set of keywords
}
}
}
return key_words; //Returns set of keywords
}
/*
std::string str = "We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (12,12); // "generalities"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
*/