-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
75 lines (69 loc) · 2.39 KB
/
util.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
#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 **/
//if product name, book author or clothing brand, keyword is a string >1 char
//if word has punctuation it should split at each puntuation
std::set<std::string> parseStringToWords(string rawWords)
{
set <string> setOfWords; //stores valid words
string storage; //stores curr letter from rawwords
for(int i =0; i<rawWords.length();i++ ){
if((ispunct(rawWords[i]) || rawWords[i]==' ') && storage.length()>1){
//if is punctuation or space and storage len makes letters in storage a valid word
setOfWords.insert(convToLower(trim(storage)));
//add lowercase storage to set
storage="";
//reset storage to empty
}
else if(rawWords[i]!=' ' && !ispunct(rawWords[i])){
//if letter is not punctuation or space
storage.push_back(tolower(rawWords[i]));
//add lowercase curr letter to storage string
}
else{
//if punctuation of space and storage len makes letters in storage an invalid word
storage="";
//make storage empty
}
}
if(storage.length()>1 || storage==""){
//if storage len greater than one or empty at end of rawWords add to set
setOfWords.insert(trim(storage));
}
return setOfWords;
}
/**************************************************
* 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));
}