模拟实现strncat

模拟实现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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值