
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - strstr() function
The C library strstr() function returns a pointer to the first index of a specified substring in another string. The main goal of strstr() is to search for a substring within a larger string and it helps to find the first occurrence of a specified substring.
Syntax
Following is the syntax of the C library strstr() function −
char *strstr (const char *str_1, const char *str_2);
Parameters
This function takes two parameters −
str_1 − This is a main string.
str_2 − The substring to be searched in main string i.e. str_1.
Return Value
The function return the value based on following conditions −
The function returns a pointer to the first characters of str_2 in str_1 or null pointer if str_2 is not found in str_1.
If str_2 is found as an empty string, str_1 is returned.
Example 1
Following is the C library program that demonstrates the substring presence within a main string using the function strstr().
#include <stdio.h> #include <string.h> int main () { const char str[20] = "TutorialsPoint"; const char substr[10] = "Point"; char *ret; // strstr(main_string, substring) ret = strstr(str, substr); // Display the output printf("The substring is: %s\n", ret); return(0); }
Output
On execution of code, we get the following result −
The substring is: Point
Example 2
Here, we demonstrate how to search the substring within a main string using the strstr() function.
#include <stdio.h> #include <string.h> int main() { char str_1[100] = "Welcome to Tutorialspoint"; char *str_2; str_2 = strstr(str_1, "ials"); printf("\nSubstring is: %s", str_2); return 0; }
Output
The above code produces the following result −
Substring is: ialspoint
Here, first four characters represents substring from a given string.
Example 3
Below the example set the if-else condition to check whether the substring present in the main string or not.
#include <stdio.h> #include <string.h> int main(){ char str[] = "It is better to live one day as a King"; char target[] = "live"; char *p = strstr(str, target); if (p) printf("'%s' is present in the given string \"%s\" at position %ld", target, str, p-str); else printf("%s is not present \"%s\"", target, str); return 0; }
Output
After executing the code, we get the following result −
'live' is present in the given string "It is better to live one day as a King" at position 16