主要考虑两点:
- 返回值对使用的便利性。
- 边界,null的判断。
strcat
char *m_strcat(char *des, const char *src)
{
assert((des != NULL) && (src != NULL));
char *add = des;
while (*des != '\0')
++des;
while (*des++ = *src++)
;
return add;
}
strlen
int m_strlen(const char *str)
{
assert(str != NULL);
int len = 0;
while (*str != '\0')
{
++str;
++len;
}
return len;
}
strcmp
int m_strcmp(const char *des, const char *src)
{
// return 0 des==src , + des>src , - des<src
assert((des != NULL) && (src != NULL));
while ((*des) == (*src))
{
if (*des == '\0')
return 0;
++des;
++src;
}
return *des - *src;
}
strcpy
char *m_strcpy(char *des, const char *src)
{
assert((des != NULL) && (src != NULL));
char *add = des;
while ((*des++ = *src++) != '\0')
;
return add;
}