模拟实现string库函数

01 模拟实现strlen函数

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
//字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
//参数指向的字符串必须要以 '\0' 结束。
//注意函数的返回值为size_t,是无符号的( 易错 )
//学会strlen函数的模拟实现
size_t my_Strlen(const char *arr)
{
	assert(arr != NULL);
	size_t count=0;
	int i = 0;
	while (arr[i++] != '\0')
	{
		count++;
	}
	return count;
}
int main()
{
	char a[10] = "wangzhen";
	printf("%d", my_Strlen(a));
	return 0;
}

02 模拟实现strcpy函数

代码:

#include<stdio.h>
#include<assert.h>
#include<string.h>
//源字符串必须以 '\0' 结束。
//会将源字符串中的 '\0' 拷贝到目标空间。
//目标空间必须足够大,以确保能存放源字符串。
//目标空间必须可变。
char* my_Strcpy(char * des, const char * sou)
{
	char * address=des;
	assert((des != NULL) && (sou != NULL));
	while(*address++ = *sou++)
	{ }
	return des;
}
int main()
{
	char a[] = "hello wangzhen";
	char b[]="hello.c";
	my_Strcpy(a, b);
	printf("%s", a);
	return 0;
}

03 模拟实现strncpy函数

代码:

#include<stdio.h>
#include<assert.h>
#include<string.h>
//拷贝num个字符从源字符串到目标空间。
//如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个
char * my_Strncpy(char * des, const char * sou, size_t num)
{
	char * address = des;
	assert((des != NULL) && (sou != NULL));
	while (num--)
	{
		*address++ = *sou++;
	}
	return des;
}
int main()
{
	char a[] = "hello wangzhen";
	char b[] = "hello.c";
	my_Strncpy(a, b, 6);
	printf("%s", a);
	return 0;
}

04 模拟实现strcat函数

代码:

#include<stdio.h>
#include<assert.h>
#include<string.h>
//源字符串必须以 '\0' 结束。
//目标空间必须有足够的大,能容纳下源字符串的内容。
//目标空间必须可修改。
//字符串自己给自己追加,如何?
char * my_Strcat(char * des, const char * sou)
{
	char* address = des;
	assert((des != NULL) && (sou != NULL));
	while (*address!='\0')
	{
		address++;
	}
	while ((*address++ = *sou++) != '\0');
	return des;
}

int main()
{
	char a[20] = "hello wang";
	char b[10] = "hello.c";
	my_Strcat(a, b);
	printf("%s", a);
	return 0;
}

遇到的问题:第一次未给字符串规定大小,导致栈溢出,编译不通过。

05 模拟实现strncat函数

代码:

#include<stdio.h>
#include<assert.h>
#include<string.h>
char * my_Strncat(char * des, const char * sou, size_t num)
{
	char* address = des;
	assert((des != NULL) && (sou != NULL));
	while (*address != '\0')
	{
		address++;
	}
	while (num--)
	{
		*address++ = *sou++;
	}
	*address = '\0';
	return des;
}
int main()
{
	char a[20] = "abcdefg";
	char b[10] = "123456789";
	my_Strncat(a, b, 6);
	//strncat(a, b, 6);
	printf("%s\n", a);
	return 0;
}

06 模拟实现strcmp函数

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<string.h>
//第一个字符串大于第二个字符串,则返回大于0的数字
//第一个字符串等于第二个字符串,则返回0
//第一个字符串小于第二个字符串,则返回小于0的数字
//那么如何判断两个字符串?
int my_Strcmp(const char * str1, const char * str2)
{
	assert((str1 != NULL) && (str2 != NULL));
	int len1 = 0, len2 = 0;
	while (*str1++)
		len1++;
	while (*str2++)
		len2++;
	return len1 - len2;
}
int main()
{
	char a[] = "hello wang";
	char b[] = "hello.c";
	printf("%d", my_Strcmp(a, b));
	return 0;
}

07 模拟实现strncmp函数

代码:

#include<stdio.h>
#include<assert.h>
#include<string.h>
int my_Strncmp(const char * str1, const char * str2, size_t num)
{
	assert(str1 != NULL && str2 != NULL&&num>=0);
	const char* pos1 = str1;
	const char* pos2 = str2;
	while (num!=0&&(*pos1==*pos2))
	{
		num--;
		pos1++;
		pos2++;
	}
		return *pos1-*pos2;
}
int main()
{
	char a[10] = "abcdefghil";
	char b[10] = "abcdefghil";
	int ret = my_Strncmp(a, b, 5);
	printf("%d\n", ret);
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RONIN_WZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值