-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cpp
48 lines (40 loc) · 1.32 KB
/
process.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
//
// Created by Derek Lin on 10/30/19.
//
#include "process.h"
void process(char * fileName , map<string,int> &m ){
// Reads file and adds to map
string word;
ifstream filestr (fileName); //declare stream variable name
// if (filestr.is_open()) {
while (filestr >> word) { //if not at end of file, continue reading
int ptr = 0;
for(unsigned int i = 0; i < word.size(); ++i)
{
if (((word[i] >= 'a' && word[i]<='z') || (word[i] >= 'A' && word[i]<='Z') || (i != 0 && word[i] == '\'') || isdigit(word[i])))
{
word[ptr] = tolower(word[i]);
ptr++;
}
}
if(word[ptr-1] == '\'') ptr--;
string strippedWord = word.substr(0,ptr);
//a, an, and, in, is, it, the
if(strippedWord == "a" ||
strippedWord == "an" ||
strippedWord == "and" ||
strippedWord == "in" ||
strippedWord == "is" ||
strippedWord == "it" ||
strippedWord == "the" ||
strippedWord == "" ) continue;
auto res = m.find(strippedWord);
if( res != m.end() ){ //Update
res->second = res->second + 1;
}
else { //Insert New
m.insert(std::pair<string,int>(strippedWord,1));
}
}
filestr.close();
}