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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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?".

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more