C how to find substring in string

To find a substring in a string in the C programming language, you can use the strstr() function. Here's an example:

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

int main() {
    char str[] = "Hello, world!";
    char substr[] = "world";

    char *result = strstr(str, substr);

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

    return 0;
}

In this example, the strstr() function is used to search for the substring "world" within the string "Hello, world!". If the substring is found, the function returns a pointer to the first occurrence of the substring within the string. The index of the substring within the string can be calculated by subtracting the starting address of the string from the result pointer. If the substring is not found, the function returns NULL.

I hope this helps! Let me know if you have any further questions.