当前位置:网站首页>Simulation Implementation of library function

Simulation Implementation of library function

2022-07-19 04:18:00 Early snow in Shaoguang

Simulation and implementation of library functions

Library functions are commonly used , Library functions are also written by many Daniel , We learn , simulation , We can see that it is so exquisite , Next , Start :


Catalog

Simulation and implementation of library functions

Preface

strcmp Function simulation implementation

strstr Function simulation implementation

memcpy Simulation Implementation

memmove Simulation Implementation

summary



Preface

This is about strings , Part of library functions , Too many library functions , This article is only a part .


strcmp Function simulation implementation

Function definition :

 

  Simulation Implementation :

#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(char* px, char* py){
	assert(px);
	assert(py);
	while (*px!='\0' || *py!='\0'){
		if (*px > *py){
			return 1;
		}
		else{
			if (*px < *py){
				return -1;
			}
		}
		px++;
		py++;
	}
	return 0;
}
int main(){
	char  arr1[10] = "abcdefg";
	char arr2[10] = "abcdeg";
	int ret = my_strcmp(arr1, arr2);
	if (ret > 0){
		printf(">");
	}
	else{
		if (ret < 0){
			printf("<");
		}
		else{
			printf("=");
		}
	}
	return 0;

}

strstr Function simulation implementation

Function definition :

 

Simulation Implementation :

#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strstr(char* px,char* py){
	int i = 0;
	int j = 0;
	while (px[i] != '\0'){
		if (px[i] == py[j]){
			char* tmp = &px[i];
			while (py[j] != '\0'){
				if (px[i] == py[j]){
					i++;
					j++;
				}
				else{
					j = 0;
					break;
				}
			}
			if (py[j] == '\0')return tmp;
		}
		else{
			i++;
		}
	}
	return NULL;
}
int main(){
	char* arr1 = "abbbbcdeeefg";
	char* arr2 = "bcd";
	char* str1 = my_strstr(arr1,arr2);
	if (str1 == NULL){
		printf(" Can't find \n");
	}
	else{
		printf("%s", str1);
	}
	return 0;
}

memcpy Simulation Implementation

Function definition :

Simulation Implementation :

#include<stdio.h>
#include<string.h>
#include<assert.h>
void* my_memcpy(void* dest,const void* src,size_t num)
{
	assert(dest && src);
	void* ret = dest;
	while (num--){
		*(char*)dest = *(char*)src;
		(char*)dest = (char*)dest + 1;
		(char*)src = (char*)src + 1;
	}
	return ret;
}
int main(){

	int arr[10] = { 0, 1, 2, 3, 4, 5 };
	int arr1[5] = { 9, 9, 9 };

	my_memcpy(arr, arr1, 12);

	for (int i = 0; i < 10; i++){
		printf("%d ", arr[i]);
	}
	return 0;
}

memmove Simulation Implementation

Function definition :

 

Function simulation :

#include<stdio.h>
#include<assert.h>
void* my_memmove(void * dest, const void * src, size_t num){
	assert(dest && src);
	void* ret = src;
	while (num--){// After judgment, it has -- 了 ,
		// Before and after 
		if (src>dest){
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
		else{
			// From back to front 
			*((char*)dest + num )=  *((char*)src+num );
			
		}
	}
	return ret;
}
int main(){
	char str[10] = "abcdefghi";
	    printf("%s\n", str);
		my_memmove(str+2, str, 3);
		printf("%s\n",str);

		return 0;
	}

summary

In the simulation implementation , There are still some parts that are not very perfect , It's not going well , In the process of simulation implementation , It is the exquisite program that I feel . There is still a long way to go , come on. !

原网站

版权声明
本文为[Early snow in Shaoguang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170313211959.html