当前位置:网站首页>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. !
边栏推荐
- Typescript数组/对象/字符串/函数参数的解构使用
- EAS(能量感知调度)绿色节能调度器
- What does the project set price mean?
- 图形验证码验证
- MAUI 框架入門學習05 MVVM數據模型理解
- 若依框架包名修改器
- [database] must know and be able at the end of the term ----- Chapter 11 concurrency control
- GNN series GCN brief derivation and understanding and DGL source code analysis
- 高性能与经济性兼备:傲腾 持久内存助力移动云应对严苛内存挑战
- C # use this keyword to concatenate constructor to call method
猜你喜欢

英特尔专家分享:如何在XPU架构上高效编程?丨至强研究所

Chapter 6 performance platform godeye source code analysis - Custom expansion module

IN Tech 2022|英特尔技术产品创新速览

leetcode7-dfs+动态规划+双指针

Machine learning 10: Integrated Learning

mqant 深入分析

C语言详解系列——循环语句的练习与巩固,二分查找的讲解

How to filter viruses / spam more effectively!

Wechat e-book reading applet graduation project (8) graduation project thesis template

EAS(能量感知调度)绿色节能调度器
随机推荐
MAUI 框架入門學習05 MVVM數據模型理解
Wechat e-book reading applet graduation design of applet completion works (2) applet function
[database] must know and be able at the end of the term ----- Chapter 11 concurrency control
[super cloud terminal to create a leading opportunity] local computing cloud management, Intel helps digitalize Education
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
Vs Code common shortcut keys
英特尔专家分享:如何在XPU架构上高效编程?丨至强研究所
[seventh issue of notebook series] download and use of openvino pre training model
How to filter viruses / spam more effectively!
Wechat e-book reading of small program graduation project (4) opening report
Wechat e-book reading applet graduation project (6) opening defense ppt
Wechat online education video on demand learning applet graduation project (4) opening report
In the era of super video, what is the solution to the data flood?
Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
To build agile teams, these methods are indispensable
VS Code 常用快捷键
小程序毕设作品之微信电子书阅读小程序毕业设计(6)开题答辩PPT
小程序毕设作品之微信电子书阅读小程序毕业设计(2)小程序功能
Structure gets the address of the main structure (struct) through member variables
Chapter 6 performance platform godeye source code analysis - Custom expansion module
