int my_strlen(char* str)
{
char* p = str;
while (*p != '\0')
{
p++;
}
return p - str;
}
char *my_strcpy(char *dest, const char*src)
{
char *ret = dest;
assert(dest != NULL);
assert(src != NULL);
while ((*dest++ = *src++))
{
;
}
return ret;
}
char *my_strcat(char *dest, const char*src)
{
char *ret = dest;
assert(dest != NULL);
assert(src != NULL);
while (*dest != '\0')
{
dest++;
}
while (*src != '\0')
{
*dest++ = *src++;
}
return ret;
}
int my_strcmp(const char* dest, const char* src)
{
int ret = 0;
assert(dest != NULL);
assert(src != NULL);
while (!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if (ret < 0)
ret = -1;
else if (ret > 0)
ret = 1;
return ret;
}
C语言模拟实现(strlen,strcmp,strcat,strcpy)函数
最新推荐文章于 2022-01-21 22:43:50 发布