C语言内置函数的简单模拟

C语言字符函数及内存函数的模拟实现

  1. strlen(取字符串长度)
size_t strlen (const char* str);

strlen函数的返回值是无符号类型的数据,参数是字符指针。
功能

  • 字符串以’\0’作为结束标志,strlen函数返回的是在字符串’\0’前面出现的字符个数(不包括’\0’).
  • 参数指向的字符串必须要以’\0’结尾。
  • strlen函数的模拟实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
int my_strlen(const char* x)
{
	assert(x);
	size_t i = 0;
	while (*x != '\0')
	{
		i++;
		x++;
	}
	return i;
}
int main()
{
	char s1[10] = "abbcdeb";
	printf("%d", my_strlen(s1));

	return 0;
}

strlen函数是我们比较常用的函数,实现比较简单,但最好模拟实现时判断传入的参数是否正确。

2.strcpy (拷贝字符串)

char* strcpy(char* destination , const char* source);
  • 源字符必须以’\0’结尾。
  • 会将源字符串中的’\0’拷贝到目标空间。
  • 目标空间足够大,以确保能够存放源字符串
  • 目标空间必须可变。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
char* my_strcpy( char* x, const char* y)
{
	char* ret = x;
	while (*x++ = *y++)
	{
		;
	}
	*x = '\0';
	return ret;
}
int main()
{
	char s1[10] = "abbcdeb";
	char s2[20] = "abb";
	printf("%s\n", my_strcpy(s1, s2));
	printf("%s\n", s1);
	return 0;
}

由于源字符串不需要改变,所以最好用const修饰一下。

  1. strcat(字符串拼接)
char* stcat(char* destination, const char*c source);
  • 源字符串必须以’\0’结束。
  • 目标空间必须有足够的大,能容纳下源字符串的内容。
  • 目标空间必须可修改。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
char* my_strcat( char* x, const char* y)
{
	char* ret = x;
	while (*x)
	{
		x++;
	}
	while (*y)
	{
		*x++ = *y++;
	}
	*x = '\0';
	return ret;
}
int main()
{
	char s1[10] = "bbc";
	char s2[20] = "abbcdeb";
	printf("%s", my_strcat(s2, s1));
	return 0;

该函数的特点是从目标字符串’\0’开始拼接,并且会复制’\0’

  1. strcmp
int strcmp (const char* str1,const char* str2);
  • 第一个字符串大于第二个字符串,则返回大于0的数字。
  • 第一个字符串等于第二个字符串,则返回0。
  • 第一个字符串小于第二个字符串,则返回小于0的数字。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* x, const char* y)
{
	assert(x);
	assert(y);
	while (*x && *y)
	{
		if (*x == *y)
		{
			x++;
			y++;
		}
		else
		{
			return  *x - *y;
		}
	}
	if (*x == '\0' && *y == '\0')
	{
		return 0;
	}
}
int main()
{
	char s1[10] = "abbcdeb";
	char s2[20] = "abbcdeb";
	if (!my_strcmp(s1,s2))
	{
		printf("字符串相等!\n");
	}
	else
	{
		printf("字符串不相等!\n");
	}
	return 0;
}

该函数的返回值并不一定确定值,在不同条件下,返回值会略有差异,但不会影响使用。

5.strstr

char * strstr( const char* str2,const char* str1);
  • 判断str2是否是str1的子串。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
char* my_strstr(const char*x,const char*y)
{
	char* dest = y;
	char* sou = x;
	char* init = x; 
	while (init)
	{
		while (*dest == *sou && init)
		{
			if (*dest != '\0')
			{
				dest++;
				sou++;
			}

		}
		if (!*dest)
		{
			return init;
		}
			init++;
			dest = y;
			sou = init;
	}
	return NULL;
}
int main()
{
	char s1[10] ="bbc";
	char s2[20] = "abbcdeb";
	if (my_strstr(s2, s1))
	{
		printf("%s", my_strstr(s2, s1));
	}
	return 0;
}

6.memcpy

void* memcpy( void* destination,const void* source,size_t num);
  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
  • 这个函数在遇到’\0’的时候并不会停下来。
  • 如果source和destination有任何的重叠,复制的结果都是未定义的。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void* my_memcpy(void* x, const void* y, size_t count)
{
	char* ret = x;
	size_t i = 0;
	char* dest = (char*)x;
	char* sou =(char*)y;
	for (i = 0; i < count; i++)
	{
		*dest = *sou;
		dest++;
		sou++;
	}
	return ret;
}
int main()
{
	int s1[10] = { 1,2,3,4,5,6,7,8,9 };
	char s2[20] = "xxxxxxxxxxxxx";
	my_memcpy(s1 + 2, s1, sizeof(s1[0]) * 4);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", s1[i]);
	}
	return 0;
}

这个函数遇到’\0’不会停止意味着复制的多少有后面的数值就决定,且遇到重叠的问题会发生错误。

  1. memmove
void * memmove( void* destination,const void* souce,size_t num);
  • 和memcpy的差别就是memmove函数处理的源内存和目标内存块是可以重叠的。
  • 如果源空间和目标空间出现重叠,就要使用memmove函数处理。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void* my_memmove(void* x,const void* y,size_t count)
{

	char* ret = (char*)y;
	char* dest = (char*)x;
	char* sou = (char*)y;
	if (dest > sou && sou + count > dest)
	{
		while (count--)
		{
			*(dest+count) = *(sou + count);
		}
	}
	else
	{
		for (size_t i = 0; i < count; i++)
		{
			*(dest + i) = *(sou + i);
		}
	}
	return ret;

}
int main()
{
	int s1[10] = {1,2,3,4,5,6,7,8,9};
	char s2[20] = "xxxxxxxxxxxxx";
	my_memmove(s1+2,s1,sizeof(s1[0])*4);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", s1[i]);
	}
return 0;
}

该函数完美解决了memcpy存在的问题。以上是简单总结的几个内置函数的模拟实现,由于本人能力有限,代码可能会有问题和繁琐之处,仅限参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值