3. “Write a function for finding out the occurences of repeated word from a sentence. This function
would return an array of duplicate words.”
#include <iostream>
#include <string>
#include <conio.h>
int count1=0;
using namespace std;
//function would return an array of duplicate words
string* findwords(string sentence,string word){
string sentencewords[100];
int count=0;
//split sentence
char* cstr = new char [sentence.size()+1];
strcpy (cstr, sentence.c_str());
char* pch= strtok(cstr," ");
while (pch != NULL){
sentencewords[count]=pch;//add word to array
count++;
pch = strtok (NULL, " ");
}
string words[100];
for(int i =0;i<count;i++){
if(strcmp(word.c_str(),sentencewords[i].c_str())==1){
words[count1]=word;
count1++;
}
}
return words;
}
//main function
int main()
{
string sentence;
string word;//word to find
cout<<"Enter sentence: ";//input sentence
getline(cin,sentence);
cout<<"Enter word: ";//input sentence
getline(cin,word);
//show result
cout<<findwords(sentence,word)[0]<<"
";
//delay
int k;
cin>>k;
return 0;
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
C++