1.strlen
实现了一个简单的字符串长度计算函数 my_strlen
,它的功能是计算以 null 结尾的字符串的长度。
int my_strlen(const char* str)
{
int idx = 0;
while ( str[idx] != '\0' ){
idx++;
}
return idx;
}
2.strcmp
实现了一个简单的字符串比较函数 my_strcmp
,它的功能是比较两个以 null 结尾的字符串,并返回它们之间的差异。
int my_strcmp(const char* s1, const char* s2)
{
int idx = 0;
while ( *s1 == *s2 && *s1 != '\0' ){
s1++;
s2++;
}
return *s1 - *s2;
}
3.strcpy
实现了一个简单的字符串复制函数 my_strcpy
,它的功能是将一个以 null 结尾的字符串从源位置复制到目标位置。
char* my_strcpy(char* dst,const char* src )
{
char* ret = dst;
while ( *src != '\0' ){
*dst++ = *src++;
}
*dst = '\0';
return ret;
}