Word Search (wordSearch.c) Write a program that uses the function speci ed below
ID: 3599090 • Letter: W
Question
Word Search (wordSearch.c)
Write a program that uses the function speci ed below to search for a given phrase
inside a given string. You may assume that the two strings will always be 100 characters
or less.
Implement this function:
int wordSearch(char *sentence, char *phrase);
The function takes a sentence and a phrase to search in the sentence. If the sentence
contains the phrase, then the function returns 1.
The main function will prompt the user to enter a sentence and a phrase to search for.
Then it will use the wordSearch function to determine if the phrase was found in the
sentence or not.
Sample Output:
Enter a sentence: My dogs name is spot.
Enter a phrase to search in the sentence: spot
The phrase "spot" was found.
Enter a sentence: I have a pet dog
Enter a phrase to search in the sentence: cat
The phrase "cat" was NOT found.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<string.h>
int wordSearch(char *sentence, char *phrase);
int wordSearch(char *sentence, char *phrase) {
int i = 0, c = 0;
while (sentence[i] != '') {
if (sentence[i] == phrase[c]) {
c++;
if (c == strlen(phrase)) {
return 1;
}
} else {
c = 0;
}
i++;
}
return 0;
}
int main() {
char word[100];
char sentence[100];
printf(" Enter a sentence: ");
fflush(stdout);
gets(sentence);
printf("Enter a word: ");
fflush(stdout);
gets(word);
int found = wordSearch(sentence, word);
if (found) {
printf("The phrase "%s" was found.", word);
} else {
printf("The phrase "%s" was NOT found.", word);
}
return 0;
}
============
Enter a sentence: My dogs name is spot.
Enter a phrase to search in the sentence: spot
The phrase "spot" was found.
Enter a sentence: I have a pet dog
Enter a phrase to search in the sentence: cat
The phrase "cat" was NOT found.
================
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.