Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ write the function The output should same as example For this question you w

ID: 3871252 • Letter: C

Question

C++ write the function The output should same as example For this question you will be extending the idea of the previous question. Now, instead of counting a single word in a single sentence, you need to write a function which prints the counts of a word across multiple sentences. countWords takes two strings as arguments and does not return any value. The first is a sequence of sentences separated by semicolons and the second is the word to match. For example: If there is snow, I think my skiing will improve; I laugh, if I watch this video; My favorite autograder of all time is: COG; void countWords(string sentences, string word //Your code here Your function should be based on the following pseudo code and should use calls to the functions defined in the previous two questions for each sentence in the sentence list output the word and the count of that word in that sentence Example Output: string sent "If there is snow, I think my skiing will improve;"+ "I laugh, if I watch this video;" + "My favorite autograder of all time is: COG;" countwords (sent, "snow) countwords (sent, "cog") countWords (sent, "i") countWords(sent, "IS") This function call should print the following table to the screen: snow 1 0 0 cog 00 1 i 1 2 IS 1 0 1 Answer: (penalty regime: 0 %)

Explanation / Answer

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;

void countWords(string sentences, string word)

{

int previous=0,current;

current = sentences.find(';');

string st="";

int count;

cout<<word<<' ';

int prv=0,crrt;

while (current != string::npos)

{

st = sentences.substr(previous, current - previous);

//cout<<st<<endl;

count = 0;

prv=0;

crrt = st.find(word);

while(crrt!=string::npos)

{

count++;

prv = crrt+1;

crrt = st.find(word,prv);

}

cout<<count<<' ';

previous = current + 1;

current = sentences.find(';', previous);

}

count = 0;

//prv=0;

st = sentences.substr(previous, current - previous);

crrt = st.find(word);

while(crrt!=string::npos)

{

count++;

prv = crrt+1;

crrt = st.find(word,prv);

}

cout<<count<<' ';

cout<<endl;

//cont.push_back(str.substr(previous, current - previous));

}

int main()

{

countWords("there is a; countdowns going on; please be on on careful; please get your on stuff;thank you","on");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote