memcpy、strstr库函数实现
#include <iostream> // 数据流输入/输出
#include <cstdio> // 定义输入/输出函数
using namespace std;
void *memcpy(void *dest, void *src, size_t count)
{
char *tmp =(char *) dest;
const char *s = (char *) src;
while (count>0)
{
*tmp++ = *s++;
--count;
}
return dest;
}
// 函数返回一个指针,它指向字符串str2 首次出现于字符串str1中的位置,如果没有找到,返回NULL。
char *strstr(const char *s1, const 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;
}
int main()
{
cout << strstr("Hello world!" ,"lo w")<< endl;
return 0;
}