The strstr () function in C

Char * function strstr (const char * haystack, const char * needle) looks for the final occurrence of the whole needle string (not including the ending null character) that is present in the haystack string.

Char * function strstr (const char * haystack, const char * needle) looks for the final occurrence of the whole needle string (not including the ending null character) that is present in the haystack string.

Declaring the function strstr () in C

Here is the declaration for strstr () in C:

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

Parameters

haystack - Chain to be scanned.

needle - A small string to be searched inside the haystack chain.

Returns the value

This function returns a pointer to the first occurrence in the haystack string of any sequence of characters defined in the needle, or function that returns a null pointer if this sequence is not present in haystack.

For example

The following C program illustrates the usage of strstr () in C:

 #include #include int  main () { const char  haystack [ 20 ] = "QTMTeam" ; const char  needle [ 10 ] = "Team" ; char * ret ;  ret  =  strstr ( haystack ,  needle );  printf ( "Chuoi con la: %sn" ,  ret ); return ( 0 ); } 

Compiling and running the above C program will result:

Picture 1 of The strstr () function in C

According to Tutorialspoint

Previous lesson: strspn () function in C

Next lesson: Strtok () function in C

You've just finished reading the article "The strstr () function in C" edited by the TipsMake team. You can save the-strstr-function-in-c.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.

« PREV : The getchar ()...
The function memcpy... : NEXT »