c strstr

c strstr() function

The strstr() function returns pointer to the first occurrence of the matched(similar) string in the given string. It is used to return the substring from first match string till the last character.

Declaration: char *strstr(const char *string,const char *match)

string : The string to be searched
match : it is The string that we need to be search in string string

return value of strstr()

This strstr() function returns the pointer to the first occurrence of the given any string, which means if we print the return value of this strstr() function, it should display the part of the main string, and start from the given string and till the end of main string.

#include<stdio.h>
#include<string.h>
int
main()
{
char str1[25]="This is my name";
char searchstring1[20]="name";
char *resultstr;
/* this function return the pointer of first occurrence of the given string (searchstring) */
resultstr=strstr(str1,searchstring1);
printf("The search substring from the given string = %s",resultstr);
return 0;
}

output:
The search substring from the given string =name

// CPP program to illustrate strstr()
#include<stdio.h>
#include<string.h>
int main()
{
// Take two in variable str1 and str2
char str1[] = "C is programming";
char str2[] = "is";
char* p;
// Here Find first occurrence of str2 in str1
p = strstr(str1, str2);
// Prints the result
if (p)
{
strcpy(p, "Strings");
printf("%s", str1);
}
else
printf("String not found\n");
return 0;
}

output:
C Strings