strstr() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The strstr function in C is defined in string.h header file. It is used to search a given substring in the main string and returns the pointer to the first occurrence of the given substring in the main string.

Syntax of strstr() Function in C

The syntax of strstr function in C is:

Here, main_str is the pointer to the character array on which the substring needs to be searched, and the substr pointer is the pointer to the string we need to search in the main string.

Note: The strstr function in C stops reading the string when it encounters a null (\0) character.

Parameters of strstr() Function in C

The strstr function in C accepts two arguments that are pointers to the character array. The first parameter (main_str) is the main string and the second parameter (substr) is the substring to be searched on the main string. The function searches for the first occurrence of substring substr in the string pointed by the pointer main_str.

Return Value of strstr() Function in C

The strstr function in C returns the pointer to the first occurrence of the given substring in the main string. If the given substring is not present in the main string then the function will return a null pointer.

Example

Let us see an example where we use the strstr() function to find the first occurrence of a string in the main string.

Output

In the above example, we used the strstr function in C to locate the first occurrence of string "weather" in the main string. Notice the function returns a pointer to the first occurrence of the keyword till the end of the string; that is why the Output from the program is "weather today?".

More Examples

Now that we have understood how the strst function in C is used in the program. Let us see an example of what the strstr() function returns when the main string has multiple occurrences of the substring or has no occurrence of the required string in the main string.

Output

Here, the strstr() function finds the first occurrence of the string "test" and returns the complete string until the end. Also, notice strstr() function finds the string "test" in the string "testing".

Now let us see a case where the given string is not present in the main string.

Output

Because the search performed by the strstr() function is case-sensitive. The required string "Testing" is not found in the main string. Hence, the function returns a NULL pointer.

Conclusion

  • The strstr function in C searches a given substring in the main string and returns the pointer to the first occurrence of the given substring in the main string.
  • To use the strstr function in the C program, we must include string.h header at the beginning of the C program.
  • If no occurrence of the required substring is found in the main string, the function returns a NULL pointer; otherwise, a character pointer is returned from the first occurrence till the end of the main string.