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.

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:

The strstr () function in C Picture 1

According to Tutorialspoint

Previous lesson: strspn () function in C

Next lesson: Strtok () function in C

5 ★ | 3 Vote

May be interested

  • The getchar () function in CPhoto of The getchar () function in C
    the function int getchar (void) in library c takes a character (an unsigned char) from stdin. this function is equivalent to the getc function with the parameter stdin.
  • Function putc () in CPhoto of Function putc () in C
    the function int putc (int char, file * stream) in library c writes one character (an unsigned char) defined by the parameter char to the given stream and increases the position indicator for that stream.
  • Putchar () function in CPhoto of Putchar () function in C
    the function int putchar (int char) in standard c library writes a character (an unsigned char) defined by the char parameter to stdout.
  • Function puts () in CPhoto of Function puts () in C
    the function int puts (const char * str) in the standard c library writes a str string to stdout (not writing null characters). a newline character (new line) is appended to the output.
  • The function ungetc () in CPhoto of The function ungetc () in C
    the function int ungetc (int char, file * stream) in the c library standard pushes the char character (an unsigned char) onto the stream given to the next character to be read.
  • The function perror () in CPhoto of The function perror () in C
    the function void perror (const char * str) in the c library standard prints a message describing the error to stderr. first, the printed string str is followed by a colon and then a space.