-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
91 lines (74 loc) · 2.48 KB
/
util.cpp
File metadata and controls
91 lines (74 loc) · 2.48 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
84
85
86
87
88
89
90
91
#include <iostream>
#include <sstream>
#include <cctype>
#include <algorithm>
#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 rawWords)
{
//set the words to be all lower case
std::string lowerWords = convToLower(rawWords);
//Create a set of type string to return
std::set<std::string> parsedWords;
//Create a temp string to look at parsed strings
std::string temp;
//Past version of 'i'
unsigned int pindx = 0;
//Traverse the entire string, looking at each char
for(unsigned int i = 0; i < lowerWords.length(); i++){
//If the char is not lowercase letter
if(lowerWords[i] < 96 || lowerWords[i] > 123){
//if(isalpha(lowerWords[i])){
//Create a subtring from the last pindx to char before the current index
temp = lowerWords.substr(pindx, i - pindx);
//Make pindx be the char after the current one
pindx = i + 1;
//If the size of the sub string is sufficient, add it to the return set
if(temp.size() > 2 ){
parsedWords.insert(temp);
}
} else if(i == lowerWords.length() -1 ){//Edge case: there is a letter @ the end of the string
//Create a subtring from the last pindx to char before the current index
temp = lowerWords.substr(pindx, lowerWords.length() - pindx);
//Make pindx be the char after the current one
pindx = i + 1;
//If the size of the sub string is sufficient, add it to the return set
if(temp.size() > 2 ){
parsedWords.insert(temp);
}
}
}
return parsedWords;
}
/**************************************************
* COMPLETED - You may use the following functions
**************************************************/
// Used from http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
// trim from start
std::string <rim(std::string &s) {
s.erase(s.begin(),
std::find_if(s.begin(),
s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
std::string &rtrim(std::string &s) {
s.erase(
std::find_if(s.rbegin(),
s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
s.end());
return s;
}
// trim from both ends
std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}