beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

C strstr() Function – C tutorial

By Chaitanya Singh | Filed Under: C library functions

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

  • Turbo C++ installation
  • First C Program
  • C - Keywords
  • 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

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap