Name your source code as .cpp (replace the text with with your last name and fir
ID: 3784702 • 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
#Include<iostream>
#include<string>
int main()
{
int word_count=0, j, len=0, frequency=0;
char str[100], str2[100],search_word[50];
cout<<" Enter the string:";
cin.getline(str,100);
for(int i=0; ((str[i]!='e')&(str[i+1]!='n')&(str[i+2]!='d'))
{
if(str[i]==' ')
{ word_count++;
}
}
cout <<"Number of words ="<< word_count+1<< end1;
cout <<"Enter the search Word";
cin.getline(search_word,50);
len =strlen(str);
while((len-3)<0)
// the input string includes "end",so we have to exclude these 3 characters
{ if(str[i]==' ')
{ if(strcmp(str2,search_word)==0)
{ frequency++;
j=0;
}
}
str2[j]=str[i];
i++;
j++;
len--;
}
cout <<"The number of occurrences of the word Is:" << frequency <<end1;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.