Write a function that simulates the STRSTR () function, and the STRSTR () function mainly returns all the strings of the main string, as well as the subsequent characters.
For example: Find BCDE in Abbcdeef, return Bcdeef
Thought:
1. Traverse the entire long string and find the same position as the short string, and record the position
2. In order to compare with the short string, if it is not the same at a later point, the position of the long string just recorded will be moved back to a further comparison;
3. Returns null if the long string traversal is not found
#include <stdio.h> #include <stdlib.h> #include <assert.h>char *my_strstr (const char *dest, const char *SRC) {assert (*dest); assert (*SRC); const char *start = Null;char *s1 = Dest;char *s2 = Src;while (*s1) {S1 = dest;//mark the location where the comparison begins S2 = Src;while ((*s1! = ') && (*s2! = ')) {if (*s1++ = = *s2++) {;} Else{dest++;break;}} if (*s2 = = ') {return dest;//returns a long string}}return NULL;} int main () {char *str1 = "Abbcdeef"; char *str2 = "BCDE"; char *ret = My_strstr (str1, str2);p rintf ("%s\n", ret); system ("Pause "); return 0;}
This article is from the "Years Quiet good" blog, please make sure to keep this source http://01160529.blog.51cto.com/11479142/1829103
Simulating the implementation of the STRSTR function