//1.模拟实现strstr函数。 #include<stdio.h> #include<assert.h> char *my_strstr(char *str, char*p) { char *cp = p; char *p1 = str; assert(*p); if (!*p) return NULL; if (!*p1) return NULL; char *p2 = str; while (*p1) { p2 = p1; cp = p; while (*p2&&*cp&&!(*p2 - *cp)) { p2++; cp++; } if (!*cp) { return p1; } p1++; } return NULL; } int main() { char str[100]; scanf("%s", &str); char *p = "zbc"; char *ret = my_strstr(str, p); printf("%s", ret); }
时间: 2024-10-01 06:55:34