C strstr Function

The C strstr function is a String Function, which returns a pointer to the first occurrence of a string in the given string. The syntax of the strstr in C Programming language is

void *strstr(const char *str, const char *str_to_look);
  • str: A valid string
  • str_to_look: String value that you want to search inside str

strstr in C Language Example

The strstr function used to search for the first occurrence of a substring within the user specified string.

You have to include the #include<string.h> header before using this string String Function. This program will help you to understand the strstr with multiple examples using If else Statement. Here, if a pointer to the given string found, then statements inside the if block will print otherwise, else block statements will print by C Programming.

/* C strstr Function example */
 
#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;
}
C strstr function example