strstr function : Finds first occurrence of sub-string in other string

Strstr function :  Finds first occurrence of sub-string in other string

Features :
  1. Finds the first occurrence of a sub string in another string
  2. Main Purpose : Finding Substring
  3. Header File : String.h
  4. Checks whether s2 is present in s1 or not
  5. On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
  6. On error (if s2 does not occur in s1), strstr returns null.
Syntax :
     char *strstr(const char *s1, const char *s2);
Live Example 1 : Parameters as String initialized using Pointers

#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.com

Live 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 & Share