[Advanced C language] Simulation implementation of major common library functions

insert image description here

foreword

What Hengchuan brings to you today is the library functions that are commonly used. Hengchuan will simulate and implement them for everyone. I hope it will be helpful to everyone! !


1. Simulate the implementation of strlen

Three ways:
Way 1:

//计数器方式
int my_strlen(const char* str)
{
    
    
	int count = 0;
	while (*str)
	{
    
    
		count++;
		str++;
	}
	return count;
}
int main()
{
    
    
	int len = my_strlen("abcdef");
	printf("%d\n", len);

	return 0;
}

Method 2:

//不能创建临时变量计数器
//递归
int my_strlen(const char* str)
{
    
    
	if (*str == '\0')
		return 0;
	else
		return 1 + my_strlen(str + 1);
}
int main()
{
    
    
	int len = my_strlen("abcdef");
	printf("%d\n", len);

	return 0;
}

Method 3:

//指针-指针的方式
int my_strlen(char* str)
{
    
    
	char* start = str;
	while (*str != '\0')
		str++;
	return str - start;//指针-指针得到的是元素个数
}

int main()
{
    
    
	int len = my_strlen("abcdef");
	printf("%d\n", len);

	return 0;
}

2. Simulate the implementation of strcpy

Reference Code:

//1.参数顺序
//2.函数的功能,停止条件
//3.assert
//4.const修饰指针
//5.函数返回值
//6.题目出自《高质量C/C++编程》书籍最后的试题部分
char* my_strcpy(char* dest, const char* src)
{
    
    
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);

	while ((*dest++ = *src++))
	{
    
    
		;
	}
	return ret;
}

3. Simulate the implementation of strcat

Reference Code:

char* my_strcat(char* dest, const char* src)
{
    
    
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	while (*dest)
	{
    
    
		dest++;
	}
	while ((*dest++ = *src++))
	{
    
    
		;
	}
	return ret;
}

4. Simulate the implementation of strstr

char* strstr(const char* str1, const char* str2)
{
    
    
	char* cp = (char*)str1;
	char* s1, * s2;
	if (!*str2)
		return((char*)str1);
	while (*cp)
	{
    
    
		s1 = cp;
		s2 = (char*)str2;
		while (*s1 && *s2 && !(*s1 - *s2))
			s1++, s2++;
		if (!*s2)
			return(cp);
		cp++;
	}
	return(NULL);
}

5. Simulate the implementation of strcmp

Reference Code:

int my_strcmp(const char* src, const char* dst)
{
    
    
	int ret = 0;
	assert(src != NULL);
	assert(dest != NULL);
	while (!(ret = *(unsigned char*)src - *(unsigned char*)dst) && *dst)
		++src, ++dst;
	if (ret < 0)
		ret = -1;
	else if (ret > 0)
		ret = 1;
	return(ret);
}

6. Simulate the implementation of memcpy

Reference Code:

void* memcpy(void* dst, const void* src, size_t count)
{
    
    
	void* ret = dst;
	assert(dst);
	assert(src);
	       /*
	        * copy from lower addresses to higher addresses
	        */
	while (count--) {
    
    
		*(char*)dst = *(char*)src;
		dst = (char*)dst + 1;
		src = (char*)src + 1;
	}
	return(ret);
}

7. Simulate memmove

Reference Code:

void* memmove(void* dst, const void* src, size_t count)
{
    
    
	void* ret = dst;
	if (dst <= src || (char*)dst >= ((char*)src + count)) {
    
    
		               /*
		                * Non-Overlapping Buffers
						*  * copy from lower addresses to higher addresses
                 */
		while (count--) {
    
    
			*(char*)dst = *(char*)src;
			dst = (char*)dst + 1;
			src = (char*)src + 1;
		}
	}
	else {
    
    
		               /*
		                * Overlapping Buffers
		                * copy from higher addresses to lower addresses
		                */
		dst = (char*)dst + count - 1;
		src = (char*)src + count - 1;
		while (count--) {
    
    
			*(char*)dst = *(char*)src;
			dst = (char*)dst - 1;
			src = (char*)src - 1;
		}
	}
	return(ret);
}

If this blog is helpful to everyone, I hope you give Hengchuan a free like as encouragement, and comment and bookmark it, thank you! ! !
It is not easy to make, if you have any questions or give Heng Chuan's opinion, welcome to leave a message in the comment area.

Guess you like

Origin blog.csdn.net/m0_75058342/article/details/129913338