#include<stdio.h>
#include<string.h>
char * strstr_1(char *s1,char *s2);
char *strstr_2(char *s1,char *s2);
char *strstr_3(char *s1, char *s2);
int main()
{
char *p;
p=strstr_1("helloworld","wor");
printf("%s\n",p);
p=strstr_2("helloworld","wor");
printf("%s\n",p);
p=strstr_3("helloworld","wor");
printf("%s\n",p);
return 0;
}
char *strstr_1(char *s1,char *s2)
{
int len2=strlen(s2);
if (!len2)
return s1;
while (*s1++)
{
if (*s1==*s2&&(strncmp(s1,s2,len2)==0))
return s1;
}
return NULL;
}
char *strstr_2(char* s1,char *s2)
{
int n,i,j;
n=i=j=0;
while(s1[i])
{
n=i;
while(s2[j]&&s1[i])
{
if(s1[i]==s2[j])
{
i++;
j++;
}
else
break;
}
if (s2[j]=='\0')
return &s1[i-j];
else
{
i=n+1;
j=0;
}
}
return NULL;
}
char * strstr_3(char *s1,char* s2)
{
int n;
if (*s2)
{
while (*s1)
{
for (n=0; *(s1 + n) == *(s2 + n); n++)
{
if (!*(s2 + n + 1))
return (char *)s1;
}
s1++;
}
return NULL;
}
else
return (char *)s1;
}
strstr--字符串处理
最新推荐文章于 2022-09-28 15:35:36 发布