The scope of the problem is this:
Search an array of string type for a value of string type that was read in from a text file and print the number of occurrences and print the number of lines that keyword occurs in.
The expected output is:
Keyword: union
Total # of keyword occurrences: 87
Total # of lines with occurrences: 29
The function I have written for this is below.
void search(int IDs[], string posts[], int size)
{
string keyword = "";
int postCount = 0;
int lineCount = 0;
int pos = 0;
bool found = false;
cout << "Enter keyword: " << endl;
cin >> keyword;
for(int i = 0; i < size; i++)
{
while((pos >= 0 && pos <= size))
{
pos = posts[i].find(keyword,pos);
lineCount++;
pos++;
}
if((pos >= 0 && pos <= size-1))
{
postCount++;
}
}
cout << "Total # of Occurances: " << lineCount + postCount << endl;
cout << "Total # of Posts: " << postCount << endl;
}
I ran the code above and got the output below:
Keyword: union
Total # of keyword occurrences: 1
Total # of lines with occurrences: 0
I asked my professor multiple times on what to do, and he told me that I must find the keyword in each line and then start at the pos+1
in the line to continue to find the next occurrences efficiently and keep track of how many times I "find" the keyword, and if I do not find it then do not count the line at all.