Write C code to implement the strstr() function to search for a substring

Question :- Write C code to implement the strstr() function to search for a substring

Synopsis:

 

Description:

The strstr function locates the first occurrence of the string string1 in the string string2 and returns a pointer to the beginning of the first occurrence.

Return Value

The strstr function returns a pointer within string2 that points to a string identical to string1. If no such sub string exists in source string a null pointer is returned.

C code to implement the strstr() function

 

 

Output

Write C code to implement the strstr() function to search for a substring

Suggested Reading

  1. Write a C program to reverse the words in a sentence in place
  2. Write your own trim() or squeeze() function to remove the spaces from a string
  3. Write a C program which does wildcard pattern matching algorithm
  • sam

    How to implement it if I have a haystack [] , needle [] and I found match , then need to store haystack values in a buffer and return 1 on match else 0 .

    haystack = “bluegrape”, needle = “grpe” returns 0
    haystack = ” grapefruit” needle = ” pef” returns 1

    I do not want to use pointers
    just arrays

    possibly a function which takes haystack[] needle[] bufferp[] and returns 1 if it is a substring else 0