BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C strstr() Function – C tutorial

By Chaitanya Singh | Filed Under: c-programming

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

C strstr() function declaration

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

str – The string to be searched.
searchString – The string that we need to search in string str

Return value of strstr()

This function returns the pointer to the first occurrence of the given string, which means if we print the return value of this function, it should display the part of the main string, starting from the given string till the end of main string.

Example: strstr() function in C

#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 that we are searching the string “you” in the string “Hello, how are you?” using the function strstr(). Since the function returned the pointer to the first occurrence of string “you”, the substring of string str starting from “you” has been printed as output.

Related Posts:

  1. C – strspn() example
  2. C – strrchr() example
  3. C – strcpy() example
  4. C – strcmp() example

Leave a Reply Cancel reply

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

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap