欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言:模拟实现strstr函数,若是子串,输出子串后面的字符串,否则输出null

程序员文章站 2022-05-13 17:32:18
#define _CRT_SECURE_NO_WARNINGS 1 #include #include #...
 #define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
char *my_strstr(char *arr1, char *arr2)
{
 assert(arr1);
 assert(arr2);
 char *s1 = arr1;
 char *p = arr2;
 char *s2 = p;
 while (*s1 != '\0')
 {
  while ((*s1 != '\0') && (*s2 != '\0') && (*s1 == *s2))
  {
   s1++;
   s2++;
  }
  if (*s2 == '\0')
  {
   return s1;
  }
 }
 return NULL;
}
int main()
{
 char *arr1 = "abcdeff";
 char *arr2 = "abc";
 char *ret = my_strstr(arr1, arr2);
 printf("%s", ret);
 system("pause");
 return 0;
}