3. “Write a function for finding out the occurences of repeated word from a sentence. This function
would return an array of duplicate words.”
1
Expert's answer
2013-06-06T08:06:43-0400
#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]<<" ";
Comments
Leave a comment