-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
59 lines (54 loc) · 1.73 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
#include <iostream>
#include <sstream>
#include <cctype>
#include <algorithm>
#include "util.h"
using namespace std;
void filter_(string&, set<string>&);//primarly and recursive function of finds a punctuation_
void del_marginpunct(string&);//deletes punctuation at end sides of the word
bool punctuation_(string&);//return 1 finds any punctuation_ and changes it from '-' to ' '
void up_to_low(string&);//uppercase to lower case
bool has_middle_punct(string); //return 0 if finds a punctuation, except for '-'
string convToLower(string src)
{
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 **/
set<string> parseStringToWords(string rawWord)
{
set<string> allwords;
filter_(rawWord, allwords);//all the work is done here
return allwords;
}
//primsrly function
void filter_(string& word, set<string> & allwords){
if(punctuation_(word)){//return 1 finds any punctuation_ and changes it from '-' to ' '
stringstream edited;
string word1;
edited << word;
while(edited >> word1)//"hello-world-blah" is now "hello world blah"
filter_(word1, allwords);//recursinve call
}
else{
if(word.size() < 2)
return;
string low_word = convToLower(word);//uppercase to lower case
allwords.insert(low_word);
}
}
//return 1 finds any punctuation_ and changes it from puntuation to ' '
bool punctuation_(string& word){
int size = word.size();
bool punctuation_ = 0;
for(int i = 1; i < size-1; i++){
if(ispunct(word[i]) != 0){
word[i] = ' ';
punctuation_ = 1;
}
else if(word[i] == ' ')
punctuation_ =1;
}
return punctuation_; //"hello-world,blah" is now "hello world blah"
}