strlen、strcpy、strcat、strcmp函数编写
作者:互联网
关于四个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;
}
标签:tmp,dest,s1,strcat,char,strcpy,str,NULL,strlen 来源: https://www.cnblogs.com/deric06/p/15036874.html