The C strstr function is a String Function that returns a pointer to the first occurrence of a string in the given string. The syntax of the strstr method is
void *strstr(const char *str, const char *str_to_look);
- str: A valid string
- str_to_look: Text that you want to search inside str
strstr function Example
The strstr function searches for the first occurrence of a substring within the user-specified string.
You have to include the #include<string.h> header before using this Function. This program will help you to understand the strstr with multiple examples using the If else Statement. Here, if a pointer to the given substring is found, then statements inside the if block will print; otherwise, else block statements will print by Programming.
#include <stdio.h>
#include<string.h>
int main()
{
char str[] = "This is abc working in abc Company";
char *res;
res = strstr(str, "abc");
if(res)
{
printf("We Found your String");
printf("\nThe Final String From 'abc' is : %s \n", res);
}
else
{
printf("String Not found. Sorry!! \n");
}
return 0;
}
