1.模拟实现strncat
//与strcat无异,只是追加的块大小不一样,strncat
只是向后追加n个字节的内
容
char *my_strncat(char * dst, const char *src,
int count)
{
char *p = dst;
while (*dst)
{
dst++;
}
while (count--) // 用数count控制循环的次数
*dst++ = *src++;
}
*dst = ‘\0‘;
return p;
}
2.模拟实现strncmp
//与strcmp无异,只是比较的块大小不一样,strncmp只是向后比较n个字节的内容
int my_strncmp(const char *src, const char
*dst, int count)
{
int ret = 0;
while (!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *src && *dst)
{
while (count--)
{
src++;
dst++;
}
if (!count)
break;
}
if (ret > 0)
ret = 1;
else if (ret<0)
ret = -1;
return ret;
}
3.模拟实现strncpy
//与strcpy无异,只是拷贝的块大小不一样,strncpy只是向后拷贝n个字节的内容
char *my_strncpy(char * dst, const char *src, int count)
{
char *p = dst;
while (count--)
{
*dst++ = *src++;
}
*dst = ‘\0‘;
return p;
}
本文出自 “零点时光” 博客,请务必保留此出处http://10741764.blog.51cto.com/10731764/1716851
原文:http://10741764.blog.51cto.com/10731764/1716851