1.strncpy() 函数的实现
man查询的:
char *strncpy(char *dest,char *src,int n)
{
int i;
for(i=0;i<n && src[i]!='\0';i++)
dest[i] = src[i];
for(;i<n;i++)
dest[i] = '\0';
return dest;
}
网上找的:
char *strncpy(char *dest,const char *src,int n)
{
int i=0;
char *ret=dest;
while(*src != '\0 ' && i <n)
{
*dest++ = *src++;
i++;
}
return ret;
}
2.strcpy() 函数的实现
static char * strcpy(char *dest, const char *src)
{
assert(dest != NULL && src != NULL);
char *ret = dest;
while ((*dest++ = *src++) != '\0');
return ret;
}
3.strcmp 函数的实现
int strcmp(const char* strLeft,const char * strRight)
{
for(;*strLeft==*strRight;strLeft++,strRight++)
{
if(*strLeft == '\0')
return 0;
}
return ((*strLeft<*strRight)? -1,1);
}
4.strdup()函数的实现
char * strdup(char *)
{
char * strNew;
assert(str != NULL);
strNew = (char *)malloc(strlen(str)+1);
strcpy(strNew,str);
return strNew;
}
5.strlen函数的实现
方法一:(标准些)
int my_strlen(const char *strDest)
{
assert(NULL != strDest);
if('\0' == *strDest)
return 0;
else
return(1+my_strlen(++strDest));
}
方法二:
int strlen(char s[])
{
int i;
i =0;
while(s[i] != '\0') ++i;
return i;
}
6.memcpy函数实现
typedef unsigned int size_t;
void * memcpy ( void *dest, const void *src, size_t num )
{
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}