A Function that Implements the strstr() function to search for a substring in a string

A Function that Implements the strstr() function to search for a substring in a string

·

1 min read

The given code implements the _strstr function that searches for a given substring needle in the main string haystack and returns the pointer to the first occurrence of the given substring in the main string.

The function first initializes two variables i and j for iterating through the haystack and needle strings respectively. Then it starts two nested loops for checking each character of the haystack and needle strings. If a character in haystack does not match the corresponding character in needle, the inner loop is broken and the outer loop continues with the next character in haystack. Otherwise, if the loop completes without any breaks, it means that the entire needle substring has been found in haystack. In that case, the function sets the pointer start to the address of the current character in haystack and returns it.

The main function uses the _strstr function to search for the substring "weather" in the string "Hello, how's the weather today?". It then prints the result depending on whether the substring was found or not.