C strstr Function Explained

In the C language, the strstr function is used to locate the first occurrence of one string within another string. The function prototype is as follows:

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

In this case, haystack is the string to search for, and needle is the substring to search for. The function will return a pointer to the position of the first occurrence of needle, or NULL if it is not found.

For example, if there is a string “hello world”, you can use the strstr function to find out if it contains the string “world”.

char *result = strstr("hello world", "world");
if (result != NULL) {
    printf("Found at position: %ld\n", result - "hello world");
}

The code above will output “Found at position: 6”, indicating that the substring “world” was found within the string “hello world” starting at position 6.

bannerAds