1.库函数

1.内存初始化

void* const mymemset(void* const pData, int val, size_t numofbytes)
{
	//让无指针类型学习
	unsigned char* const _pData = (unsigned char* const)pData;
	for (size_t i = 0; i < numofbytes; ++i)
	{
		_pData[i] = val;
	}
	return pData;
}

2.内存拷贝

void* const mymemcpy(void* const pDest, void const* const pSrc, size_t numofbytes)
{
	if (pDest != pSrc)
	{
		//让无指针类型学习
		unsigned char* const p1 = (unsigned char* const)pDest;
		unsigned char const* const p2 = (unsigned char const* const)pSrc;
		if (p1 < p2)
		{
			for (size_t i = 0; i < numofbytes; ++i)
			{
				p1[i] = p2[i];
			}
		}
		else {
			for (int i = numofbytes - 1; i >= 0; --i)
			{
				p1[i] = p2[i];
			}
		}
	}
	return pDest;
}

3.内存比较

int mymemcmp(void const* const pBuff1, void const* const pBuff2, size_t numofbytes)
{
	if (pBuff1 != pBuff2)
	{
		unsigned char const* const p1 = (unsigned char const* const)pBuff1;
		unsigned char const* const p2 = (unsigned char const* const)pBuff2;
		for (size_t i = 0; i < numofbytes; ++i)
		{
			if (p1[i] > p2[i])
			{
				return 1;
			}
			if (p1[i] < p2[i])
			{
				return -1;
			}
		}
	}
	return 0;
}

4.求字符串长度

size_t mystrlen(char const* const pstr)
{
	int index = 0;
	while (pstr[index] != '\0')
	{
		++index;
	}
	return index;
}

5.字符串拷贝

char* const mystrcpy(char* const pDest, char const* const pSrc)
{
	if (pDest != pSrc)
	{
		if (pDest < pSrc)
		{
			int index = 0;
			while (pDest[index] = pSrc[index])
			{
				++index;
			}
		}
		else {
			int srclen = mystrlen(pSrc);
			//从'\0'开始拷贝
			for (int i = srclen; i >= 0; --i)
			{
				pDest[i] = pSrc[i];
			}
		}
	}
	return pDest;
}

6.字符串比较

int mystrcmp(char const* const pstr1, char const* const pstr2)
{
	if (pstr1 != pstr2)
	{
		int index = 0;
		while (pstr1[index] != '\0' || pstr2[index] != '\0')
		{
			if (pstr1[index] > pstr2[index])
			{
				return 1;
			}
			if (pstr1[index] < pstr2[index])
			{
				return -1;
			}
			++index;
		}
	}
	return 0;
}

7.字符串连接

char* const mystrcat(char* const pDest, char const* const pSrc)
{
	char* const _pDest = pDest + mystrlen(pDest);
	if (_pDest < pSrc)
	{
		int index = 0;
		while (_pDest[index] = pSrc[index])
		{
			++index;
		}
	}
	else {
		int srclen = mystrlen(pSrc);
		for (int i = srclen; i >= 0; --i)
		{
			_pDest[i] = pSrc[i];
		}
	}
	return pDest;
}

8.查找字符串中的一个字符,并返回第一个字符的地址

char* const mystrchr(char const* const pstr, int key)
{
	int index = 0;
	while (pstr[index]!='\0')
	{
		if (pstr[index] == key)
		{
			return (char* const)(pstr + index);
		}
		++index;
	}
	return nullptr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

所恋皆洛尘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值