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() 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
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:
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;
}
You can also try this code with Online C Compiler
Run Code
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() function in C
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
#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;
}
You can also try this code with Online C Compiler
Run Code
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
#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;
}
You can also try this code with Online C Compiler
Run Code
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 the strstr() function in C, a built-in function used to locate substrings within a string. We also discussed its significance and demonstrated how to implement it in C programs to make code more efficient and readable by simplifying string search operations.
To learn more about functions in c, check out the following articles.