Hey all,
I'm working on this homework assignment and I'm really having trouble. I'm supposed to count the number of words more than two characters(have to contain one letter), unique words, and the number of times each unique word appears in the Programming Execution Environment. I'm also supposed to get input to search for in the PEE and output the number of times it appears and the line where it appears. I have some of it working, but I'm really struggling with counting how many times each word appears. I know my code is really bad right now, but that's why I'm here. Any help is really appreciated!
struggling with counting how many times each word appears
Look into std::map. It holds a pair of values: unique key and value which can be accessed by key.
so if you have std::map<std::string, int> word_count; each time you have a word you want to count in variable word you can just do word_count[word] += 1; If word didn't appear before, operator[] will assign 0 to value of key word and after inctrement we will have 1, what we actually want.
I know my code is really bad right now, but that's why I'm here. Any help is really appreciated!
If you show which code was provided by intructor and you must absolutely use that, we might ompimize other code. And tell, does your compiler support C++11, some faculties would be useful here.
//this code given by my instructor
externchar **environ; // needed to access your execution environment
int k = 0;
size_t wordCount = 0;
while (environ[k] != NULL)
{
cout << environ[k] << endl;
string str(environ[k]);
envstr = envstr + str;
k++;
}
That's the part I have to use. I'm trying to change the code to use the map, but I'm still trying to figure it out because I've never used that before. My functions to separate the words is wrong, but if I can get that right I think I could get the map to work too. I'm using Visual Studios 2013, which I'm pretty sure supports C++11.
b) I do not see what is word. Does path like C:\ProgramData is one word or ProgramData is one? Or mabe whole line ALLUSERSPROFILE=C:\ProgramData is one word, seeing like there is no spaces?
From the assignment: A word is any string of at least two characters, i.e., letters, and/or digits, and/or
underscores “_”. To be a word, the string must have at least one letter. Thus x86 is a
word while 86 and C: are not words. Any other characters including <newline> and
whitespace should be interpreted as delimiter separators between words and non-words
(or words). For example, the string “SystemRoot C:\Users” consists of 2 words, where
SystemRoot is a word and Users is a word. C:\Users is not a word because the characters
“:” and “\” are not legal word characters. How do I split it so there are delimiters between the words? I want to try and split the string, but how do I do that if there are multiple delimiters? Like :, =, \, etc
Thanks, that really helped. I've got it working for the most part. The only thing left is the searching. I search through and store the positions in a vector, so then how do I output them? I need to output the whole line where the string occurs. Here's what I have for that so far:
string input;
while (!input.compare("END") && !input.compare("\n"))
{
cout << "Enter your search item: \n";
cin >> input;
//string can only be forty characters
if (input.length() > 40 || input == "\n")
{
cout << "That search query is too long. \n";
continue;
}
//change the search string to lowercase, like the envstr
transform(input.begin(), input.end(), input.begin(), tolower);
int j = 0;
int searchCount = 0;
vector<size_t> positions;
size_t pos = envstr.find(input, 0);
//search for that string
while (pos != string::npos)
{
positions.push_back(pos);
pos = envstr.find(input, pos + 1);
searchCount++;
}
cout << "\nThat phrase occurs a total of " << searchCount << " times.\n";
cout << "It occurs in the following lines: \n";
//output where that string occurs
for (vector<size_t>::iterator it = positions.begin(); it != positions.end(); ++it)
{
unsigned i = *it;
while (envstr[i] != '\n' && i < envstr.size() - 1)
{
cout << envstr[i];
i++;
}
cout << endl;
}
positions.clear();
}
But this isn't right. I think it outputs more lines than it should
Problem is probably that if word is in line multiple times, it will be outputted multiple times. I suggest to check one line at a time and make sure it pushed only once: http://ideone.com/qKJo1X