★ Create a function to implement the function of library function strstr #include<stdio.h> #include <stdlib.h> #include <assert.h>char *my_strstr ( Char *src,char *dst) {assert (DST), assert (SRC), char *p = Src;char *q = Dst;while ((*DST) && (*SRC)) {if (*src = = *DST) When two pointers are equal, the pointer moves backwards to match {src++;d st++;} ELSE{SRC = ++p; When the pointer content is unequal, the substring pointer jumps to the first address, and the original string returns to the next position where the match begins DST = q;p = src;}} if (*dst = = ' + ')//substring is ' \ n ', the description match is completed and the original string is returned with the same initial address as the substring {return p;} Else{return NULL; There is no match, return empty}}int main () {char arr1[] = "ABBAAC", char arr2[] = "ABBA", char *ret = MY_STRSTR (arr1, arr2);p rintf ("%s\n", ret); System ("pause"); return 0;}
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/75/A2/wKiom1Y_CRnhzJG7AAAWYbK1_UU058.png "title=" Run Result " alt= "Wkiom1y_crnhzjg7aaawybk1_uu058.png"/>
This article is from the "Warm Smile" blog, please be sure to keep this source http://10738469.blog.51cto.com/10728469/1710723
Create a function that implements the function of the library function strstr