c语言 strstr和strrpl库函数的作用是什么??

来源:百度知道 编辑:UC知道 时间:2024/05/14 02:38:48

标准C语言实现下列标准库函数,设计中不得使用其他库函数。
strstr库函数:

char *strstr(char *str1,char *str2);
在字符串str1中,寻找字串str2,若找到返回找到的位置,否则返回NULL。

#include <iostream>

char *strstr(const char *str1, const char *str2);

char *strstr(const char *str1, const char *str2)
{
char *s1, *s2;
assert ((str1 != (char *)0) && (str2 != (char *)0));

/* 空字符串是任务字符串的子字符串 */
if ('\0' == *str2)
{
return ((char *)str1);
}

while (*str1)
{
s1 = (char *)str1;
s2 = (char *)str2;

while ((*s1 == *s2) && *s1 && *s2)
{
s1++;
s2++;
}
if ('\0' == *s2)
{
return ((char *)str1);
}
str1++;
}
/* 查找不成功,返回NULL */
return ((char *)0);
}