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

Name your source code as .cpp (replace the text with with your last name and fir

ID: 3786149 • Letter: N

Question

Name your source code as .cpp (replace the text with with your last name and first name, in that order) and upload to blackboard. Write a program to read a number of words from the user until the user inputs the word "end*" (with those asterisks). Once done, read another word from the user. Let's call this word "SearchWord". The goal of the program is to find the number of occurrences of the SearchWord in the words that the user input earlier. Your search should be case-sensitive. Assume no punctuation in the input.

Explanation / Answer


// C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <vector>

using namespace std;

int main()
{
std::vector<string> words;
string word;

while(true)
{
cout << "Enter word: ";
cin >> word;

if(word == "end")
break;
else
words.push_back(word);
}

string search;
cout << "Enter a word to search: ";
cin >> search;

int count = 0;
for (int i = 0; i < words.size(); ++i)
{
if(words[i] == search)
count++;
}
  
cout << search << " occured " << count << " times in the list of words ";
}

/*
output:

Enter word: my
Enter word: name
Enter word: is
Enter word: ayush
Enter word: verma
Enter word: I
Enter word: ayush
Enter word: go
Enter word: to
Enter word: school
Enter word: my
Enter word: my
Enter word: my
Enter word: native
Enter word: end
Enter a word to search: my

my occured 4 times in the list of words


*/

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