Latest web development tutorials

C library functions - strstr ()

C standard library - <string.h> C standard library - <string.h>

description

C library functionschar * strstr (const char * haystack , const char * needle) in the string haystackto find theneedlein the first position of the first occurrence of the string, not including the terminating character '\ 0'.

statement

Here is () statement strstr function.

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

parameter

  • haystack - C strings to be retrieved.
  • needle - in the haystack string to search for small strings.

return value

This function returns the position of the first occurrence of needle in the haystack string if not found it returns null.

Examples

The following example demonstrates the strstr () function is used.

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


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

   ret = strstr(haystack, needle);

   printf("子字符串是: %s\n", ret);
   
   return(0);
}

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

子字符串是: School

C standard library - <string.h> C standard library - <string.h>