Site icon Codeforcoding

strstr string function in C programming language

strstr string function in C programming language

In this tutorial, We will learn about the strstr string function in C programming language and its functionality.

Description

strstr() – In the c programming language, the strstr function is used to locate the first occurrence of the substring pointed by the str2 within str1 string.

Declaration

Syntax for strstr() function is given below

char *strstr( const char *str1, const char *str2);

parameter or argument

str1 – This string finds from str2

str2 – The substring from where its found.

Returns

The strstr() function pointer looks for the first occurrence of the substring(str2) within the string pointed by s1. If s2 can’t find it, it returns a null pointer.

Required Header

the required header for strstr() String function in C Programming language

#include<string.h>

Example

Program 1

“this is the best website for learning to code” is a string . “for” is a substring for find first occurrence in the string.

#include <stdio.h>
#include <string.h>
int main()
{
    char myweb[100]="this is the best website for learning to code";
    char *p;
    p=strstr(myweb,"for");


       printf("%s\n",p);
getch();
    return 0;
}

 

When above program me  is compiled it will produce the following result

for learning to code

 

 

There are other C programming language functions that are  similar to the this function

strncmp() function

 strcat() function

strcpy() function

strncpy() function

 

Suggested for you

String handling in C language

String function in C Language

Function in C language

Arithmetic function in C language

strrchr string function in C programming Language
strcmp string function in C programming Language
Exit mobile version