C Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 5)
Which of the following function is used to find the first occurrence of a given string in another string?
Answer: Option

The function strstr() Finds the first occurrence of a substring in another string

Declaration: char *strstr(const char *s1, const char *s2);

Return Value:
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.


#include <stdio.h>
#include <string.h>

int main(void)
   char *str1 = "IndiaBIX", *str2 = "ia", *ptr;

   ptr = strstr(str1, str2);
   printf("The substring is: %s\n", ptr);
   return 0;

Output: The substring is: iaBIX

10 comments Page 1 of 1.

Rakhi Kirtankar said:   5 years ago
Strstr function finds one string in another string. And when it finds the second string it prints that substring along with the remaining character. And pointer is used for storing returned address

Arun said:   5 years ago
How could we solve the program without using strstr fuctions?

Surbhi said:   6 years ago
char *strstr(const char * string, const char* substr).

It will return a pointer to the first occurrence of sub string thus will act as character array.

Ramya said:   7 years ago
How it prints "iabix". Can someone explain clearly?

Nikhil said:   8 years ago
ia search in indiabix string from ia it will print reaming print iabix.

Kd said:   8 years ago
How can we say that output is a BIX, because there already given strr1 = IndiaBix. How it's possible?

Manisha said:   9 years ago
This function finds the second string matches with the first string it returns to pointer location otherwise 0.

Srinivas said:   1 decade ago
The strstr() function finds the first occurrence of the substring in given string.

So it will return the pointer(Address) where there is matching of substring occurs.

Nancy said:   1 decade ago
How iaBIX s output what happen to india how strstr() removes india.??

Jalal said:   1 decade ago
Why use pointer function?

Post your comments here:

Your comments will be displayed after verification.