C strstr Function

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 C 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 in C Language 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;
}
C strstr example

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.