首页 > 其他 > 详细

模拟实现strncat

时间:2015-11-25 19:37:23      阅读:154      评论:0      收藏:0      [点我收藏+]

模拟实现strncat:

在系统库函数中,存在strncat这个函数,它用于字符串的追加,就是在一个字符串后面再追加一个字符串,它的函数原型为:

char *strncat( char *strDest, const char *strSource, size_t count );

在其中,*strDest为目标字符串,*strSource为源字符串,count为需要追加的字符串的个数,strncat就是将源字符串追加count个字符在目标字符串后面。size_t表示无符号整型,因为count不可能为负数。同样也可以自己定义:

typedef unsigned int nuit;

模拟代码实现 :

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef unsigned int uint;
char *my_strncat(char *dest, const char *src, uint count)//模拟实现strncat函数
{
	assert(dest);
	assert(src);
	int *ret = dest;
	while (*dest)
	{
		dest++; //找到dest中的\0
	}
	while (count--)
	{
		*dest++ = *src++;
	}
	*dest = ‘\0‘;
	return ret;
}
//程序测试
int main()
{
	char arr[20] = "abcdef";
	int len = strlen(arr);
	my_strncat(arr, arr, len);
	printf("%s\n", arr);
	system("pause");
	return 0;
}


本文出自 “Stand out or Get out” 博客,请务必保留此出处http://jiazhenzhen.blog.51cto.com/10781724/1716814

模拟实现strncat

原文:http://jiazhenzhen.blog.51cto.com/10781724/1716814

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!