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

Can someone help me understand the following solution to this problem in C++ programming?

ID: 3549756 • Letter: C

Question


Write a program (starting from #include) that gets a keyword from the
user and then also gets a phrase. The program should then output the number of times
that the keyword appeared in their phrase, repeating their keyword in quotes. You may
assume the keyword search is case-sensitive. That is, if the keyword is "the", then we
will not count any occurrence of "The" or "THE". A sample run is shown below.

Enter a keyword: the
Enter a phrase: The lord of the rings is the best movie!
The keyword "the" appeared 2 times in your phrase.

The part I get confused about is when it starts saying in the solution I got
"int searchStart = 0;
int numOccur = 0;
while ( searchStart <= phrase.length( ) - keyword.length( ) ) {
if ( phrase.substr( searchStart,
keyword.length( ) ) == keyword ) {
numOccur ++;
searchStart += keyword.length( );
}
else
searchStart ++; "

the following is the solution I obtained


SOLUTION:
# include <iostream>
# include <string>
using namespace std;

int main ( ) {
string keyword;
string phrase;
cout << "Enter a keyword: ";
cin >> keyword;
cout << "Enter a phrase: ";
getline(cin,phrase);
int searchStart = 0;
int numOccur = 0;
while ( searchStart <= phrase.length( ) - keyword.length( ) ) {
if ( phrase.substr( searchStart,
keyword.length( ) ) == keyword ) {
numOccur ++;
searchStart += keyword.length( );
}
else
searchStart ++;
}
cout << "The keyword "" << keyword << "" appeared "
<< numOccur << " times in your phrase. ";
return 0;
} Can someone help me understand the following solution to this problem in C++ programming? Write a program (starting from #include) that gets a keyword from the
user and then also gets a phrase. The program should then output the number of times
that the keyword appeared in their phrase, repeating their keyword in quotes. You may
assume the keyword search is case-sensitive. That is, if the keyword is "the", then we
will not count any occurrence of "The" or "THE". A sample run is shown below.

Enter a keyword: the
Enter a phrase: The lord of the rings is the best movie!
The keyword "the" appeared 2 times in your phrase.

The part I get confused about is when it starts saying in the solution I got
"int searchStart = 0;
int numOccur = 0;
while ( searchStart <= phrase.length( ) - keyword.length( ) ) {
if ( phrase.substr( searchStart,
keyword.length( ) ) == keyword ) {
numOccur ++;
searchStart += keyword.length( );
}
else
searchStart ++; "

the following is the solution I obtained


SOLUTION:
# include <iostream>
# include <string>
using namespace std;

int main ( ) {
string keyword;
string phrase;
cout << "Enter a keyword: ";
cin >> keyword;
cout << "Enter a phrase: ";
getline(cin,phrase);
int searchStart = 0;
int numOccur = 0;
while ( searchStart <= phrase.length( ) - keyword.length( ) ) {
if ( phrase.substr( searchStart,
keyword.length( ) ) == keyword ) {
numOccur ++;
searchStart += keyword.length( );
}
else
searchStart ++;
}
cout << "The keyword "" << keyword << "" appeared "
<< numOccur << " times in your phrase. ";
return 0;
}

Explanation / Answer

// Simple dude

let take an example


Enter a keyword: the
Enter a phrase: The lord of the rings is the best movie!


Now see,

so how will you proceed if u find keyword in given string,

the substr function takes 2 parameters, the first is the position from where you start extracting and the 2nd is thelength of the word to be extracted.

find the next occurence in the string after this occurence so given was

searchStart += keyword.length( );

else

search from next element..

why

if you have


keyword - aah

phrase - aaahaahnji


you have to check in first case if aah not equal to aaa(aaah), match to aah aah(aaah). infact as in match words you can better increase by a space.


the above given is standard algo for o(n^2) search algorithm.



so the first parameter must not exceed the value of length of the phrase - length of the text because then it would start to extract the characters beyond the length of the phrase.

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