Strstr function : Finds first occurrence of sub-string in other string
Features :
- Finds the first occurrence of a sub string in another string
- Main Purpose : Finding Substring
- Header File : String.h
- Checks whether s2 is present in s1 or not
- On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
- On error (if s2 does not occur in s1), strstr returns null.
Syntax :
char *strstr(const char *s1, const char *s2);
#include<stdio.h>#include<string.h>
int main(void){ char *str1 = "c4learn.blogspot.com", *str2 = "spot", *ptr; ptr = strstr(str1, str2); printf("The substring is: %sn", ptr); return 0;}Output :
The substring is: spot.comLive Example 2: Passing Direct Strings
#include<stdio.h>#include<string.h>
void main(){ char *ptr; ptr = strstr("c4learn.blogspot.com","spot"); printf("The substring is: %sn", ptr);}Output :
The substring is: spot.com
Bookmark & ShareIncoming search terms:
- array string pointer strstr c (1)
- c it finds the first occurrence of the first string in the ather and return a pointer (1)
- c programming strstr (1)
- he function that finds the first occurrence of a given string in another string i (1)
- how to write a program of strstr in c without using pointers (1)
- in strstr() in c programmingp=strstr(str[i] str1; (1)

