Question

Good Day,

So I decided to go trough my C again and started making a simple search the word in a string.

Here is my code:

#include<stdio.h>
#include<conio.h>
#include<string.h>

main(){
char word[100];
char sentence[100];

clrscr();
printf("Enter a word: ");
fgets(word, 100, stdin);
getch();
printf("The word is : %s", &word);
getch();
printf("\nEnter a sentence: ");
fgets(sentence, 100, stdin);
getch();
printf("The sentence is: %s",&sentence);
getch();
if(strstr(sentence,word) == 0){
  printf("word found!");
}
getch();
return 0;
}

The problem now is that whenever i try to search for the word in a string using strstr, it always returns word found. I also tried using strcmp but that will only compare the first instance of the string and stops when a match is not found so its not really advisable if you want to do a word searching in a string problem.

I haven't really made a program like this before, never needed one actually. So may I ask why is not working properly since according to its description strstr should be to search for a word in a sentence or did I just misunderstood it.

Also if you have any comments on my program feel free to say so, so that I may become aware of my mistake.

Thank you

Example: word: dog
sentence: the dog is here
should return true

Was it helpful?

Solution

This line

if(strstr(sentence,word) == 0)

should be

if(strstr(sentence,word) != NULL)

strstr() returns a pointer to the first occurence of word in sentence or NULL if the word was not found.

For details please read here.


Also using fgets() to read in the "strings" appends a '\n' to the end of the "strings". This prohibits successful comparsion.

To chop of the trailing '\n' you could do like this:

fgets(word, sizeof(word), stdin);
{
  size_t size = strlen(word);

  if (size && ('\n' == word[size-1]))
  {
    word[size-1] = '\0';
  }
}

OTHER TIPS

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

Returning NULL (0) essentially means that the string was NOT found.

Thus

if(strstr(sentence,word) == 0){
  printf("word found!");
}

Should be

if(strstr(sentence,word) != NULL){
  printf("word found!");
}

Source - http://en.cppreference.com/w/c/string/byte/strstr

Additionally as Armin points out, you shouldn't use the address operator, &, in your printf. %s expects to have a pointer passed to printf and using the name of the array as sentence is actually the same as a pointer to the start of the array, i.e sentence == &sentence[0]

The strstr() function finds the first occurrence of the substring "needle" in the string "haystack" and it returns the pointer to the beginning of the substring or NULL if not found.

so your if statement should be

if(strstr(sentence,word) != NULL){
printf("word found");
}

Array name represents base address of its own, so writing

printf("%s",&word) 

or

printf("%s",word) 

both repesents base address of array.

There is a little correction in your code i.e.,

if(strstr(sentence,word) == 0){
 printf("word found!");
}

Change is: instead ==0 to !=0 which is equivalent to !=NULL, in case of pointers 0 and NULL are same. because NULL keyword is defined as:

#define NULL ((void *)0)

so change your code to

if(strstr(sentence,word) != 0){
 printf("word found!");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top