Simulate to implement strstr, query whether the target string contains the searched substring. The main point is to record the current pointer position when the target string moves. In this article, cur is used to record.#include <stdio.h><br/>#include<assert.h><br/>char* my_strstr(char* p1,char* p2)<br/>{<br/>assert(p1 && p2);<br/>char* cur = p1;<br/>char* s1 = p1;<br/>char* s2 = p2;<br/>if (*s2 == '\0')<br/>return p1;<br/>while (*cur)<br/>{<br/>s1 = cur;<br/>s2 = p2;<br/>while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)<br/>{<br/>s1++;<br/>s2++;<br/>}<br/>if (*s2 == '\0')<br/>{<br/>return cur;<br/>}<br/>cur++;<br/>}<br/>return NULL;<br/>}<br/>int main()<br/>{<br/>char arr1[] = "abbcdef";<br/>char arr2[] = "bcd";<br/>char* ret=my_strstr(arr1, arr2);<br/>if (ret == NULL)<br/>printf("找不到子串");<br/>else<br/>printf("是子串");<br/>}
Simulation implementation library function strstr and simulation implementation function memcpy
Guess you like
Origin blog.51cto.com/14736509/2489252
Ranking