当前位置:网站首页>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
strcmp Function simulation implementation
strstr Function simulation implementation
memcpy Simulation Implementation
memmove Simulation Implementation
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. !
边栏推荐
- 小程序毕设作品之微信电子书阅读小程序毕业设计(8)毕业设计论文模板
- 小程序毕设作品之微信电子书阅读小程序毕业设计(2)小程序功能
- 小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(2)小程序功能
- JS array processing [slice implements the deletion, insertion and replacement of arrays]
- minimum spanning tree
- Small program completion work wechat online education video on demand learning small program graduation design (2) small program function
- 英特尔+联想共同推出开源云解决方案
- In tech 2022 | Intel technology product innovation quick view
- 小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(4)开题报告
- High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges
猜你喜欢

Wechat Online Education video on Demand Learning of applet Graduation Design (3) Background Function

如何更有效的过滤病毒/垃圾邮件!

可省近90%服务器,反欺诈效率却大增,PayPal打破「AI内存墙」的方案为何如此划算?

【微信小程序】超易懂的条件渲染和列表渲染

Wechat e-book reading of small program graduation project (4) opening report

Wechat official account page authorization 40029 error "suggested collection"

06 MAUI,WPF使用 MVVM Toolkit 框架 构建 MVVM 程序

Machine learning 11: cost sensitive learning

SQL interface switching cannot obtain focus

基于stm32f103的智能风扇系统
随机推荐
小程序毕设作品之微信电子书阅读小程序毕业设计(4)开题报告
小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(1)开发概要
Cocos creator 3.0 foundation - event system
ffmpeg中AVFrame\AVPacket与自己的数据交互
Dapr series (I)
PAC十年:见证HPC从CPU时代走向XPU纪元
[database] must know and be able at the end of the term ----- Chapter VIII database security
基于stm32f103的智能风扇系统
How does the enterprise post office set up SPF records?
Leetcode7 DFS + dynamic programming + double pointer
若依框架包名修改器
The adaptation of go language under windows10:vscode
Openresty as a static resource server
Chapter 1 performance platform godeye source code analysis - overall architecture
SQL interface switching cannot obtain focus
论文精读系列文章
英特尔专家分享:如何在XPU架构上高效编程?丨至强研究所
【微信小程序】超易懂的条件渲染和列表渲染
V4L2学习资料收集
XDC 2022 Intel 技术专场:英特尔软硬件技术构筑云计算架构基石
