strstr,memmove,memcpy和strcmp的模拟实现

一、strcmp()
1、标准规定
第一个字符串大于第二个字符串,则返回大于0的值
第一个字符串小于第二个字符串,则返回小于0的值
第一个字符串等于第二个字符串,则返回0
2、模拟实现

int strcmp(const char* str1, const char* str2)
{
	assert(str1 != '\0' && str2 != '\0');//设置断言,确保str1和str2不是空值,若是空值。系统会报错
	while (str1 != '\0' && str2 != '\0')
	{
		if (*str1 > *str2)
		{
			return 1;
		}
		else if (*str1 < *str2)
		{
			return -1;
		}
		else
		{
			str1++;
			str2++;
		}
	}//此时从上个循环跳出,说明两个指针中的一个已经指到了\0,此时进一步比较
	if (*str1 < *str2)
	{
		return -1;//这种情况是str1先到\0,则str1小
	}
	else if (str1 > str2)
	{
		return 1;//这种情况是str2先到\0,则str1小
	}
	else
	{
		return 0;//这种情况是相等
	}
}

二、strstr()
1、用法:
strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。
找到所搜索的字符串,则该函数返回第一次匹配的字符串的地址;
如果未找到所搜索的字符串,则返回NULL。
2、模拟实现

const char* strstr(const char* str, const char* aim)
{
	assert(str != NULL && aim != NULL);//设置断言,确保str1和aim不是空值,若是空值。系统会报错
	if (aim == '\0')
	{
		return NULL;
	}
	const char* black = str;
	while (*black != '\0')
	{
		const char* red = black;
		const char* a = aim;
		while (*red == *a && *a != '\0' && *black != '\0')
		{
			red++;
			a++;
		}
		if (*a == '\0')
		{
			return black;
		}
		else if (*black == '\0')
		{
			return NULL;
		}
		else
		{
			black++;
		}
	}
	return NULL;
}

三、memcpy()
1、功能
由sou所指内存区域复制num个字节到des所指内存区域。
2、位于string.h头文件里,但与字符串无关系,拷贝的是内存,可以拷贝任何类型的变量类型
3、该函数在遇到’\0’的时候不会停下来
4、如果sou和des有任何重叠,复制结果都是未定义的
5、模拟实现

void* memcpy(void* des, const void* sou, size_t num)
{
	assert(des != NULL && sou != NULL);
	char* d = (char*)des;
	char* s = (char*)sou;
	while (num--)
	{
		*d = *s;
		d++;
		s++;
	}
	return des;
}

在这里插入图片描述
四、memmove()
1、功能
memmove用于拷贝字节,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中,但复制后源内容会被更改。但是当目标区域与源区域没有重叠则和memcpy函数功能相同。
2、和memcpy()区别
memcpy处理的源内存块和目标内存块是可以重叠的
3、目标实现

void* memmove(void* des, const void* sou, size_t num)
{
	assert(des != NULL && sou != NULL);
	char* d = (char*)des + num - 1;
	char* s = (char*)sou + num - 1;
		while (num--)
		{
			*d = *s;
			d--;
			s--;
		}
	return des;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. atof(): 将字符串转换为double类型的值。 例如: ```c++ char str[] = "3.14"; double num = atof(str); printf("%f", num); ``` 结果为:3.140000 2. atoi(): 将字符串转换为int类型的值。 例如: ```c++ char str[] = "1234"; int num = atoi(str); printf("%d", num); ``` 结果为:1234 3. atol(): 将字符串转换为long类型的值。 例如: ```c++ char str[] = "1234567"; long num = atol(str); printf("%ld", num); ``` 结果为:1234567 4. strtod(): 类似于atof(),将字符串转换为double类型的值。 例如: ```c++ char str[] = "3.14"; double num = strtod(str, NULL); printf("%f", num); ``` 结果为:3.140000 5. strtol(): 将字符串转换为long类型的值,同时支持指定转换的基数(例如10进制、16进制等)和错误检查。 例如: ```c++ char str[] = "0110"; long num = strtol(str, NULL, 2); printf("%ld", num); ``` 结果为:6 6. strtoul(): 类似于strtol(),不过返回的是无符号的long类型。 例如: ```c++ char str[] = "0xA"; unsigned long num = strtoul(str, NULL, 16); printf("%lu", num); ``` 结果为:10 7. memset(): 将一段内存区域设置为指定的值。 例如: ```c++ char str[10]; memset(str, 'a', sizeof(str)); printf("%s", str); ``` 结果为:aaaaaaa 8. memcpy(): 将一段内存区域的内容复制到另一段内存区域。 例如: ```c++ char src[] = "hello"; char dst[10]; memcpy(dst, src, sizeof(src)); printf("%s", dst); ``` 结果为:hello 9. memmove(): 和memcpy()类似,但是保证在有重叠的情况下会正确工作。 例如: ```c++ char str[] = "hello"; memmove(str + 2, str, 3); printf("%s", str); ``` 结果为:hehlo 10. memcmp(): 比较两段内存区域的内容是否相等。 例如: ```c++ char str1[] = "hello"; char str2[] = "Hello"; int result = memcmp(str1, str2, 5); printf("%d", result); ``` 结果为:32(h和H的ASCII码差值) 11. memchr(): 在一段内存区域中搜索指定的字符,并返回指向该字符的指针。 例如: ```c++ char str[] = "hello"; char* ptr = (char*)memchr(str, 'l', 5); printf("%s", ptr); ``` 结果为:ll 12. strcpy(): 将一个字符串复制到另一个字符串。 例如: ```c++ char src[] = "hello"; char dst[10]; strcpy(dst, src); printf("%s", dst); ``` 结果为:hello 13. strncpy(): 类似于strcpy(),不过只会复制指定长度的字符。 例如: ```c++ char src[] = "hello"; char dst[10]; strncpy(dst, src, 3); dst[3] = '\0'; printf("%s", dst); ``` 结果为:hel 14. strcat(): 将一个字符串附加到另一个字符串的末尾。 例如: ```c++ char str1[] = "hello"; char str2[] = "world"; strcat(str1, str2); printf("%s", str1); ``` 结果为:helloworld 15. strncat(): 类似于strcat(),不过只会附加指定长度的字符。 例如: ```c++ char str1[] = "hello"; char str2[] = "world"; strncat(str1, str2, 3); printf("%s", str1); ``` 结果为:helloworld 16. strcmp(): 比较两个字符串是否相等。 例如: ```c++ char str1[] = "hello"; char str2[] = "world"; int result = strcmp(str1, str2); printf("%d", result); ``` 结果为:-15 17. strncmp(): 类似于strcmp(),不过只会比较指定长度的字符。 例如: ```c++ char str1[] = "hello"; char str2[] = "world"; int result = strncmp(str1, str2, 3); printf("%d", result); ``` 结果为:0 18. strchr(): 在一个字符串中搜索指定的字符,并返回指向该字符的指针。 例如: ```c++ char str[] = "hello"; char* ptr = strchr(str, 'l'); printf("%s", ptr); ``` 结果为:llo 19. strrchr(): 类似于strchr(),不过会从字符串的末尾开始搜索。 例如: ```c++ char str[] = "hello"; char* ptr = strrchr(str, 'l'); printf("%s", ptr); ``` 结果为:lo 20. strstr(): 在一个字符串中搜索指定的子字符串,并返回指向该子字符串的指针。 例如: ```c++ char str[] = "hello world"; char* ptr = strstr(str, "world"); printf("%s", ptr); ``` 结果为:world
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值