1.模拟实现strcat–字符串追加
void my_strcat(char* p1, char* p2)
{
assert(p1 != NULL);
assert(p2 != NULL);
//1.找到串1的''
while (*p1++);
p1--;
while (*p1++ = *p2++);
}
int main()
{
char arr1[20]= "abcdef++++++++";
char arr2[] = "abcd";
my_strcat(arr1, arr2);
printf("%sn", arr1);
}
2.模拟实现strstr–是否包含字串
char* my_strstr(const char* p1, const char* p2)
{
assert(p1 != NULL);
assert(p2 != NULL);
if (!*p2)
return (char*)p1;
char* s1=(char*)p1;
char* s2 = (char*)p2;
while (*s1++ != *p2);
s1--;
char* cur = s1;
while (*p1++){
s1 = cur;
s2 = (char*)p2;
while (*s1++ == *s2++)
{
if (!*s2)
return cur;
}
cur++;
}
return NULL;
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net