字符串函数模拟实现代码及思路(2)(strncpy、strncat、strncmp )

一、前言

我们在之前模拟了一些基本的字符串函数,但是strcpy、strcat、strcmp等函数存在一些隐患。
因为它们判断结束的标志都是’\0’,同时,不会检查目标缓冲区的长度。如果源字符串长度超过目标缓冲区,会导致缓冲区溢出,进而可能导致程序崩溃或安全漏洞。
所以,我们今天的函数模拟相当于对上次的更新。
此外,我们仍需要i注意如果源字符串长度大于目标缓冲区长度,strncpy 不会在目标字符串末尾添加空字符,这可能会导致程序出现错误。因此,在使用 strncpy等函数时,应该格外小心,并确保源字符串长度不会超过目标缓冲区长度。

二、字符串函数的模拟实现

根据上次模拟实现库函数的经验,我们可以知道如果想要限制函数的访问空间,我们可以给函数增加一个参数,这个参数可以是我们选择要复制或者增加的长度空间。
有了上面的思路,我们就可以更好的写下面的库函数。

  • 使用assert的时候,一定要调用assert.h

1.strncpy

这是strncpy函数的声明:

char * strncpy ( char * destination, const char * source, size_t num );

  • 第一步,两个指针参数,第一个是目标地址,第二个是源地址。num则是我们选择要复制的字符串长度。返回参数是一个指针,我们可以将复制结束的源地址返回回去。
  • 将两个指针参数传递过来以后,我们要把目标地址所对应的内容复制给源地址,然后两个地址同时加1,再复制,直到复制够了num个字符结束。
char* my_strncpy(char* destination, const char* source, size_t num)
{
	assert(destination && source);
	char* ret = destination;
	while (num--)
	{
		*destination++ = *source++;
	}
	return ret;
}
int main()
{
	char arr1[10] = { 0 };
	char arr2[] = "victoryabc";
	my_strncpy(arr1, arr2, 7);
	printf("%s", arr1);
	return 0;
}

运行结果:
在这里插入图片描述

2.strncat

strncat函数声明:

char * strncat ( char * destination, const char * source, size_t num );

  • 这个函数的参数跟上面strncpy一样,它的作用是在字符串后面添加新的一串字符。
  • 如果我们要在字符串的后面添加新的字符串,我们首先就是要找到这个字符串的最后一位(即’\0’所对应的地址)。然后,将源地址的字符串首位赋值给’\0’,两个指针同时加1,直到添加够了num个字符结束。
char* my_strncat(char* destination, const char* source, size_t num)
{
	assert(destination && source);
	char* ret = destination;
	while (*destination)
		destination++;
	while (num--)
	{
		*destination++ = *source++;
	}
	return ret;
}
int main()
{
	char arr1[20] = "abcdef ";
	char arr2[] = "victory";
	my_strncat(arr1, arr2, 7);
	printf("%s", arr1);
	return 0;
}

运行结果:
在这里插入图片描述

3.strncmp

strncmp函数声明:

int strncmp ( const char * str1, const char * str2, size_t num );

  • 这个函数的参数是跟前面一样的,但是它的返回值是一个整形。
  • 我们使用这个函数是用来比较字符串大小的,如果str1跟str2一样,我们可以返回0。如果str1小,则返回-1,str1大返回1。
  • 这个库函数的原理是先比较两个指针所对应字符的ASC||码的大小,如果第一位相同,那么就比下一位。直到我们比够了num位就可以结束。
int my_strncmp(const char* str1, const char* str2, size_t num)
{
	assert(str1 && str2);
	while (num--)
	{
		if (*str1 > *str2)
			return 1;
		else if (*str1 < *str2)
			return -1;
		else
		{
			str1++;
			str2++;
		}
	}
	return 0;
}
int main()
{
	char arr1[20] = "victora";
	char arr2[] = "victory";
	int ret = my_strncmp(arr1, arr2, 6);
	printf("%d", ret);
	return 0;
}

运行结果:
这里我们可以看到
这里我们可以看到两个字符串的第七位是不一样的,但是我们只比较了前六位,因此,返回值是0.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值