C String strstr()

The strstr() function in the C programming language is used to find the first occurrence of a substring within a larger string. It stands for “string search” and is declared in the string.h header file.

The syntax of strstr() is as follows:

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

The function takes two arguments: haystack and needle. haystack is the main string in which you want to search for the substring, and needle is the substring you want to find.

The function returns a pointer to the first occurrence of the substring needle within the haystack string. If the substring is not found, the function returns a null pointer (NULL).

Here’s an example usage of strstr():

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

int main() {
    const char* haystack = "Hello, world!";
    const char* needle = "world";

    char* result = strstr(haystack, needle);

    if (result != NULL) {
        printf("Substring found at index: %ld\n", result - haystack);
    } else {
        printf("Substring not found.\n");
    }

    return 0;
}

In this example, the haystack string is “Hello, world!” and the needle is “world”. The strstr() function is used to search for the substring “world” within the haystack string. If the substring is found, the index of the first occurrence is printed. Otherwise, a message indicating that the substring was not found is printed.

The output of this example will be:

Substring found at index: 7

which indicates that the substring “world” was found at index 7 within the haystack string.

String strstr() parameters:

The strstr() function in C takes two parameters:

  1. haystack: This parameter is a pointer to a null-terminated string in which you want to search for the substring. It represents the larger string within which you are looking for a match.
  2. needle: This parameter is a pointer to a null-terminated string that represents the substring you want to find within the haystack string.

The function searches for the first occurrence of the needle substring within the haystack string and returns a pointer to the beginning of the matching substring. If the substring is not found, the function returns a null pointer (NULL).

Here’s the syntax of strstr():

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

Note that the haystack and needle parameters are of type const char*, indicating that they are pointers to constant character arrays (strings). The function does not modify the contents of the strings.

It’s important to ensure that both the haystack and needle strings are null-terminated, meaning they are terminated with a null character ('\0') at the end. Otherwise, the behavior of strstr() will be undefined.

String strstr() example:

Certainly! Here’s an example that demonstrates the usage of the strstr() function:

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

int main() {
    const char* haystack = "I love programming with C!";
    const char* needle = "programming";

    char* result = strstr(haystack, needle);

    if (result != NULL) {
        printf("Substring found at index: %ld\n", result - haystack);
        printf("Matched substring: %s\n", result);
    } else {
        printf("Substring not found.\n");
    }

    return 0;
}

In this example, we have a haystack string that says “I love programming with C!” and a needle string that is “programming”. The strstr() function is used to search for the substring “programming” within the haystack string.

If the substring is found, the program will print the index of the first occurrence of the substring using (result - haystack) and also print the matched substring using result. If the substring is not found, a message indicating that the substring was not found will be printed.

The output of this example will be:

Substring found at index: 7
Matched substring: programming with C!

This indicates that the substring “programming” was found at index 7 within the haystack string, and the matched substring is “programming with C!”.