C

Function Strstr() In C – C Tutorial

The strstr() function searches the specified main string for the given string and returns the pointer to the first occurrence of the string.

Strstr() function declaration in C

char *strstr(const char *str, const char *searchString)

str – The string to search for.
searchString – The string to be searched in string str.

Strstr’s return value ()

This function returns the pointer to the first occurrence of the given string, which means that if we print the return value of this function, it should display the portion of the main string beginning with the given string and continuing until the end of the main string.

In C, for example, the strstr() function

#include <stdio.h>
#include <string.h>
int main () {
   const char str[20] = "Hello, how are you?";
   const char searchString[10] = "you";
   char *result;

   /* This function returns the pointer of the first occurrence
    * of the given string (i.e. searchString) 
    */ 
   result = strstr(str, searchString);
   printf("The substring starting from the given string: %s", result);
   return 0;
}

Output:

The substring starting from the given string: you?

As you can see, we are using the function strstr to find the string “you” within the string “Hello, how are you?” (). Because the function returned the pointer to the first occurrence of string “you,” the substring of string str beginning with “you” was printed as output.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button