Thursday, February 4, 2025

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: %s\n", 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: %s\n", ptr);
}

Output  :
   The substring is: spot.com

Bookmark & Share
Want more Articles in Your Email ??

Subscribe

( Right click to Enter Email )
Enter your email address :


Check Your Email After Clicking on Subscribe Button

Bookmark and Share

0 comments:

Post a Comment

Your Feedback : This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

Previous Next home