C库函数strncat实现

#include 

#define BUFF_SIZE	(1024)

char *mystrncat(char *dest, const char *src, unsigned int len)
{
     
	char *p = dest;
	int index;

	while ('\0' != *p)
		p ++;
	
	for (index = 0; '\0' != src[index] && index < len; index ++)
		p[index] = src[index];
	p[index] = '\0';
	
	return dest;
}

int main(int argc, const char *argv[])
{
     
	char *str = "1234567890";
	char buff[BUFF_SIZE] = "HELLO";

	mystrncat(buff, str, 5);
	puts(buff);

	return 0;
    
}


测试结果

在这里插入图片描述

你可能感兴趣的:(C,linux,c,strcat,strncat,指针)