例如:给定s1 = AABCD和s2 = BCDAA,返回1,给定s1=abcd和s2=ACBD,返回0.
#include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> char *my_strncat(char *dest, const char *src, size_t count) { assert(dest); assert(src); char *ret = dest; while (*dest) { dest++; } while (count--) { *dest++ = *src++; } *dest = ‘\0‘; return ret; } char *my_strstr(char *str1, const char *str2)// { char*ptr = str1; char*p1 = NULL; char*p2 = NULL; while (*ptr) { p1 = ptr; p2 = str2; while (*p1 == *p2) { p1++; p2++; if (*p2 == ‘\0‘) { return ptr; } } ptr++; } return NULL; } int main() { char arr[20] = "abcdef";//预留空间 char *p = "cdefab"; int len = strlen(arr); my_strncat(arr, arr, len); char*ch=my_strstr(arr, p); if (ch != NULL) { printf("%p\n", ch); printf("%s", ch); } else { printf("not exit"); } system("pause"); return 0; }
时间: 2024-10-12 12:43:59