strstr() in C Language – String library Function

Spread the love
strstr-function-in-c-language-with-example-programs

Introduction:

We have covered the strlen, strchr, strcpy, strcat, and strcmp library functions in our earlier posts. Today’s post will look at the strstr() in C programming language.

Prerequisites:

In order to better understand the article, Please go through the following tutorials regarding the Strings in C programming.

The strstr() Function in C:

The strstr() function is used to search a substring in a string. So if we want to search for a string in another string, we can use the strstr() function.

The strstr() functiron searches for the substring in the main string and returns the pointer to the first occurrence of the substring.

Syntax of strstr() function in C Language:

Here is the syntax of the strstr() in c language:

The strstr() function searched for the subString in the mainString.

Parameters:

The strstr() function takes two strings as the parameter.

  • mainString – The main string where the search happens.
  • substring – String to be searched in the mainString.

Return type:

The return type of the strstr() function is char *.

If the substring is found in the mainString, Then the strstr() function returns the character pointer pointing to the first occurrence of the substring in the mainString.

If the substring is not found in the mainString, Then strstr() function returns NULL.

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

Let’s look at an example program about strstr() function.

Program to search for a sub-string in a string using the strstr() function in C Language:

Here is the program to search for a sub string in a string using strstr() function in C.

⚠️We need to include the string.h header file to use the strstr() function.

Program Explanation:

Here is the step-by-step explanation of the finding a substring program

  1. Declare two strings mainStr and subStr.
    • The mainStr is the main string, This is string where we are going to search for the subStr.
    • The subStr is the sub string. String to be searched in the mainStr
  2. Take the user input for the mainStr and subStr strings. We are using the gets() function. but gets() function won’t do the boundary checks, so use it with causion. Please look at the Different Ways to Read and Print strings in C programming to learn about other ways to read the strings.
  3. Now we got the Main string ( mainStr) and the Sub string ( subStr). To search for the string subStr in the mainStr string we use the strstr() function.
  4. Call the strstr() function and pass the mainStr as first argument and the subStr as the second argument. The strstr() function returns the pointer to the first occurrence subStr string in the Main string( mainStr). – res = strstr(mainStr, subStr);
  5. If the subStr is found in the mainStr, Then res will contain some address (Won’t be NULL). So print the “FOUND” message on console.
  6. If the subStr is not found in the mainStr, Then the return value res will contain the NULL. So display the “NOT FOUND” message on the console

Program Output:

let’s compile and RUn the program using the GCC compiler ( Any compiler)

Test Case 1: When the substring is found in the main string.

strstr-function-in-c-program-output

In the above example, The main string is Who likes formula One? and the sub string is formula. As the sub string formula is found in the main string. The strstr() function returned the valid address pointing to the first occurrence of the formula in the main string.

Test 2: When the sub string is not found in the Main string

In the above example, The substring Hello is not found in the main string Howdy, Welcome to Sillycodes.com. So the strstr() function returned the NULL as the return value.

Program to understand the return value of the strstr() function:

Let’s modify the above program to print the result if we find the substring in the main string. This will prove us that the strstr function is returning the address of first occurrence of the sub string in the main string.

Her is the rewritten version of the above program.

Program Output:

Compile and Run the program

The main string is One Two One Two One Two and the substring is Two.

The strstr() function found the first occurrence of the substring( Two) in the main string and returned the character pointer pointing to it (i.e res pointer)

If we print the resultant pointer, We will get Two One Two One Two as the string. Which confirms us the strstr() function returned the first occurrence of the Two substring.

Related Tutorials:

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. […] strstr library function in C […]

Leave a Reply