关于四个strlen,strcpy,strcat,strcmp函数的编写方法
#include <assert.h>
size_t str_len(const char* str)
{
assert(NULL != str);
const char* tmp = str;
while(*tmp) tmp++;
return tmp - str;
}
char* str_cpy(char* dest,const char* src)
{
assert(NULL!=dest && NULL!=src);
char* tmp = dest;
while(*tmp++ = *src++);
return dest;
}
char* str_cat(char* dest,const char* src)
{
assert(NULL!=dest && NULL!=src);
char* tmp = dest;
while(*tmp)tmp++;
while(*tmp++ = *src++);
return dest;
}
int str_cmp(const char* s1,const char* s2)
{
assert(NULL!=s1 && NULL!=s2);
while(*s1 == *s2 && *s1) s1++,s2++;
if(*s1 > *s2)
return 1;
if(*s1 < *s2)
return -1;
return 0;
}