'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Last Updated: Jan 19, 2024
Easy

strstr() Function in C

gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

In this blog, we will discuss the strstr function in c language. The strstr function in c is one of the built-in library functions that will try to understand its importance in C programming and how we can implement the strstr function in c programs.

strstr Function in C

What is strstr() function in C?

In programming, you might face some problems where you need to check whether a string is a substring in another string or you need to return a pointer for the substring in another available string. In these scenarios, you will surely need the strstr function in c. 

An strstr function in c is a predefined or built-in function that gives us the pointer to the first index of the substring in another string.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of strstr() Function in C

Char *strstr(const char *string2, const char *string1)


Here string2 is the substring that will get searched in string1.

Parameters of strstr() Function in C

The strstr() function in C is used to find the first occurrence of a substring within a string. It has the following parameters:

char *strstr(const char *haystack, const char *needle);


haystack: This is a pointer to the string in which you want to search for the substring. It is the main string within which the needle will be searched.

needle: This is a pointer to the substring you want to search for within the haystack string. It is the pattern or subsequence that you want to find.

The strstr() function returns a pointer to the first occurrence of the needle within the haystack string. If the needle is not found, it returns a null pointer (NULL).

Also see: C Static Function

What is the return value of strstr() in C?

The strstr() function in C returns a pointer to the first occurrence of the substring within the string. The return value can be one of the following:

  1. If the substring is found within the string, strstr() returns a pointer to the first character of the found substring within the original string.
     
  2. If the substring is not found within the string, strstr() returns a null pointer (NULL).

Example for return value of strstr() in C

Let us see an example below:

  • C

C

#include <stdio.h>
#include <string.h>

int main() {
char *temp1="Hello";
char *temp2="l";
char *temp3="_";
char *res1;
char *res2;
res1=strstr(temp1, temp2);
res2=strstr(temp1, temp3);
printf("%s \n", res1);
printf("%s", res2);

return 0;
}

 

Output

llo 
(null)

 

We can see that since l is found in Hello, the pointer pointing to its first occurrence is returned. But, since _ is not found, (null) is returned.

More Example

String1 = “Hello, welcome to coding ninjas.”

String2 = “ninjas”
 

strstr(String2, String1)

Output = ninjas


As we have mentioned above strstr function in c will return a pointer to the first occurrence of substring String1 in String2, and when it prints the result of the strstr function, it will print out the whole substring.

Importance of strstr

Before we see the implementation of the strstr function in c, let’s understand its importance and how much impact it can provide in a code.

Naive Approach of strstr() in C

The following program is the naive approach to knowing whether a string is a substring of another string. This naive approach will compare the two string characters by character and increase the count variable by one every time it matches a character.

The count will be zero again if the characters are not matching. If the count variable is equal to the size of the substring, that will mean string2 is the substring of string1. Here string1 and string2 are the names for string variables, respectively.

  • C

C

#include <stdio.h>
#include <string.h>
int main() {
 char * string1;
 char * string2;
 char * output;
 int count = 0, j;
 /* Declaring values of string1 and string2 */
 string1 = "coding ninjas";
 string2 = "coding";

 for (j = 0; j < string1[j] != '\0'; j++) {
   if (string2[count] != '\0') {
     if (string1[j] == string2[count]) {
       count++;
     } else {
       count = 0;
     }
   }
   if (count == strlen(string2)) {
     break;
   }
 }
 if (count == strlen(string2)) {
   printf("string2 is a substring of string1");
 } else {
   printf("string2 is not a substring of string1");
 }
 return 0;
}


Output:

string2 is a substring of string1


The time complexity of the above program is O(n*m), where n is the size of string1 and m is the size of string2. For a huge string, this program will be a complete disaster. Also, we can observe how complex our programs look with so many conditions.

To solve these problems, we will now see the implementation of the strstr function in c.

Implementation of strstr Function in C

When implementing the strstr function or any string-related built-in functions, you need to include the <string.h> as a header file. Now let's see the implementation of the strstr function in c and observe the difference between this program and the program we discussed above.

  • C

C

#include <stdio.h>
#include <string.h>
int main() {
 char * string1;
 char * string2;
 char * output;

 /* Declaring values of string1 and string2 */
 string1 = "welcome to coding ninjas";
 string2 = "ninjas";

 /* Using strstr function searching string2 in string1 as substring*/
 output = strstr(string1, string2);

 /* Using strcmp() function to compare the values of output and string2
 if both are equal, then string2 is a substring of string2; else, not*/
 if (strcmp(string2, output) == 0) {
   printf("string2 is a substring of string1");
 } else {
   printf("string2 is not a substring of string1");
 }
 return 0;
}


Output:

string2 is a substring of string1


By comparing both programs, we can see how simpler it is to understand the program with the strstr function compared to another one. We use the strcmp( ) function to compare the resultant string and given substring string2. The strcmp( ) will return 0 if strings are equal; else not, it is another built-in function in c.

We hope you understand how and why to use the strstr function in c if you want to find out whether a string is a substring of another string.

Check out this problem - Multiply Strings

Frequently Asked Questions

What is the function of Strnchr in C?

The strnchr() function is not a standard function in C. But, you can use strchr() function to find the first occurrence of a character in a string,

What is the strrchr function in C?

The strrchr() function in C is responsible for finding the last occurrence of a character in a string, returning a pointer to it or NULL if not found.

What is the output of strstr in C?

In C, the strstr function is used to find the first occurrence of a substring within another string. If the substring is found, strstr returns a pointer to the first character of the substring in the original string; otherwise, it returns a null pointer (NULL). The output is the pointer to the substring or NULL.

Is there any function declared as strstr ()?

Yes, the strstr() function is declared in the C programming language's string.h header. It finds the first occurrence of a substring within a string, returning a pointer to the substring or NULL if not found.

Conclusion

In this article, we learned about the strstr function in c. An strstr function in c is one of the built-in functions available in c. we have also discussed the importance of the strstr function and how we can implement it in our c programs, making our program less complex and readable.

To learn more about functions in c, check out the following articles.

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Learning!

Previous article
What is the purpose of a function prototype?
Next article
Static functions in C
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass