1.模拟实现strcpy
2.模拟实现strcat
3.模拟实现strstr
4.模拟实现strcmp
char* my_strcpy(char* str,const char* p)
{
assert(str != NULL);
assert(p != NULL);
char*s = str;
while ((*s++ = *p++) != '\0')
{
;
}
return s;
}
char*my_strcat(char*str,const char*scr)
{
assert(str != NULL);
assert(scr != NULL);
char*ret = str;
while (*str != '\0')
{
str++;
}
while (*str++ = *scr++)
{
;
}
return ret;
}
char*my_strstr(const char*str, const char*scr)
{
const char*s1 = str;
const char*s2 = scr;
const char*cp = s1;
while (*cp != '\0')
{
s1 = cp;
s2 = scr;
while (*s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)cp;
}
cp++;
}
return NULL;
}
int my_strcmp(const char*s1, const char*s2)
{
while (*s1 == *s2)
{
if (*s1 == '\0')
{
return 0;
}
s1++;
s2++;
}
return *s1 - *s2;
}
2.模拟实现strcat
3.模拟实现strstr
4.模拟实现strcmp
char* my_strcpy(char* str,const char* p)
{
assert(str != NULL);
assert(p != NULL);
char*s = str;
while ((*s++ = *p++) != '\0')
{
;
}
return s;
}
char*my_strcat(char*str,const char*scr)
{
assert(str != NULL);
assert(scr != NULL);
char*ret = str;
while (*str != '\0')
{
str++;
}
while (*str++ = *scr++)
{
;
}
return ret;
}
char*my_strstr(const char*str, const char*scr)
{
const char*s1 = str;
const char*s2 = scr;
const char*cp = s1;
while (*cp != '\0')
{
s1 = cp;
s2 = scr;
while (*s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)cp;
}
cp++;
}
return NULL;
}
int my_strcmp(const char*s1, const char*s2)
{
while (*s1 == *s2)
{
if (*s1 == '\0')
{
return 0;
}
s1++;
s2++;
}
return *s1 - *s2;
}