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:
If the substring is found within the string, strstr() returns a pointer to the first character of the found substring within the original string.
- 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:
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.
String1 = “Hello, welcome to coding ninjas.”
String2 = “ninjas”
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.
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.
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.
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.