Custom strstr function implementation in C Language

Spread the love
own-strstr-function-implementation-in-C-programming-language

Program Description:

We are going to look at strstr function implementation in c programming language. As we already know the strstr()function is used to search for a substring in the string. The program will accept two strings from the user, one for the main string and one for the substring. It searches for the substring in the main string and returns the pointer to the first occurrence substring.

📢 This Program is part of the C String Practice Programs series

Here is the example input and output of the program.

Program Input and Output:

Input:

Enter a main string : we pass through this world but once

Enter the substring(String to search in main string): this

Output:

FOUND! Substring:'this' is found in mainString at 'this world but once'

The substring this is found in the main string and the program is returned the pointer to it.

Prerequisites:

We are going to use the String, Functions, and arrays in C language. So please go through the following articles to refresh your concepts.

strstr() function syntax:

Before looking at the custom implementation of strstr() function, Here is the prototype of the strstr() library function

The strstr() function is available as part of the string.h and The strstr() function searches for the substring( subStr) in the main string( mainStr) and returns the pointer to the first occurrence of the substring.

If the subStr is found in the mainStr, It returns the character pointer to the first occurrence of the subStr. If the subStr is not found, Then it returns a NULLpointer.

So we need to create a custom strstr() function which takes two character pointers as formal arguments and returns a character pointer.

Custom strstr function implementation in C Program Explanation:

Let’s look at the step-by-step instructions to implement the custom strstr() function

  1. Start the main() function by declaring the two strings and let’s say the main string as mainStr and the substring as subStr. The size of both strings is equal to SIZE macro.
  2. Read the two strings from the user and store them in mainStr and subStr respectively. We use the gets function to read the strings. The gets() function doesn’t do the boundary checks so be cautious while using the gets function.
  3. Now to search for the substring in the main string, We are going to create a function called mystrstr. The mystrstr() function takes two character pointers( char *mainStr, char *subStr) and returns a character pointer. – i.e char * mystrstr(const char * mainStr, const char * subStr)
  4. Within the mystrstr() function, To search for the substring, Iterate over the main string( mainStr) until the *mainStr reaches the terminating NULL character( NULL). – i.e while( *mainStr != NULL)
    • At each iteration, Check if the current character of mainStr is equal to the first character of the subStr ( i.e (*mainStr == *subStr))
    • If the above condition is true, Then search for all characters of the subStr in the mainStr. We need to use another while loop to iterate over the contents of the mainStr and subStr. We are using two temporary pointers s1 and s2 to compare the characters.
      • Check if the *s1(s1 = mainStr)is not matching to *s2 ( s2 = substr), If so then the subString is not found. Otherwise, continue to check all elements of the subStr by incrementing the s1 and s2 pointers.
    • Once the above while loop is completed, Check if we iterated over all elements of the substring ( use * s2 == NULL ). If we iterated the complete substring( subStr), then we found the substring in the main string. return the mainStr pointer( char *) back to the caller.
  5. Call the mystrstr() function from the main() function and pass the main string and substring and store the result on the result pointer – i.e char * result = mystrstr(mainStr, subStr);
  6. Finally check the result pointer and display the results on the console.

Program to implement custom strstr() function C Language:

Let’s convert the above logic into the C code.

Here is the custom implementation of the strstr() function in c.

Here are the details of the custom strstr() function( mystrstr function).

  • char * mystrstr(const char * mainStr, const char * subStr)
  • Function Name mystrstr
  • Arguments list – (char *, char *)
  • Return Valuechar *

The mystrstr() function searches for the subStr in the mainStr and returns the pointer to the first occurrence of the subStr in the mainStr. If the subStr is not found, Then it returns the NULL.

Program Output:

Let’s compile and run the program.

Test Case 1: Positive case:

custom-strstr-function-implementation-in-C-language

The substring this is found in the string and the mystrstr() function returned the character pointer to first occurrence of the substring this. So the resultant string is this world but once.

Let’s try another example.

Here are also, The substring programming is found in the main string and the resultant string is programming online at sillycodes.com'

Test Case 2: Negative Cases:

In the above example, The substring push is not found in the main string Git rebase. So the mystrstr() function returned the NULL pointer back to the main() function.

Related String Practice Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Custom strstr() function implementation in C […]

Leave a Reply