What is Strstr in C?


C String strstr()

The strstr() function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character.

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

int main () {
    const char haystack[20] = "TutorialsPoint";
    const char needle[10] = "Point";
    char *ret;
    ret = strstr(haystack, needle);
    printf("The substring is: %sn", ret);
    return(0);
}

String Functions in C

The string.h header file supports all the string functions in C language.

C String functions:

  • strstr() – Returns pointer to first occurrence of str2 in str1
  • Other 17 functions

strstr() in C++

The strstr() function is a predefined function in string. It is used to find the occurance of a substring in a string. This process of matching stops at ” and does not include it.

Function strspn()

The strspn() function returns the length of the initial substring of the string pointed to by str1 that is made up of only those characters contained in the string pointed to by str2.

Function str()

The str() function converts the specified value into a string.

Lascia un commento