Easy Tutorial
❮ C Function System C Examples Concatenate String ❯

C Library Function - strstr()

C Standard Library - <string.h>

Description

The C library function char *strstr(const char *haystack, const char *needle) finds the first occurrence of the substring needle in the string haystack, not including the terminating null character '\0'.

Declaration

Here is the declaration for the strstr() function.

char *strstr(const char *haystack, const char *needle)

Parameters

Return Value

The function returns a pointer to the first occurrence of the needle substring in the haystack string, or null if the substring is not found.

Example

The following example demonstrates the use of the strstr() function.

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

int main()
{
   const char haystack[20] = "tutorialpro";
   const char needle[10] = "NOOB";
   char *ret;

   ret = strstr(haystack, needle);

   printf("The substring is: %s\n", ret);

   return(0);
}

Let's compile and run the above program, which will produce the following result:

The substring is: NOOB

C Standard Library - <string.h>

❮ C Function System C Examples Concatenate String ❯