Implement strstr() function in C | Iterative & Recursive implementation

Write an efficient function to implement strstr function in C. strstr() function returns a pointer to the first occurrence of a string in another string.

 
 

The prototype of strstr is:

const char* strstr(const char* str1, const char* str2);

The strstr() function returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

 

Iterative Implementation (Naive):

C

Download   Run Code

Output:

Coding made easy

Java

Download   Run Code

Output:

Coding made easy

Recursive Implementation (Efficient):

C

Download   Run Code

Output:

Coding made easy

Using memcmp() function:

C

Download   Run Code

Output:

Coding made easy

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz