当前位置:网站首页>库函数的模拟实现(strlen)(strcpy)(strcat)(strcmp)(strstr)(memcpy)(memmove)(C语言)(VS)
库函数的模拟实现(strlen)(strcpy)(strcat)(strcmp)(strstr)(memcpy)(memmove)(C语言)(VS)
2022-08-01 12:10:00 【星尘不会落】
文章目录
前言
这是我对库函数的模拟实现,希望对大家有帮助。
一、模拟实现strlen

#include<stdio.h>
#include<assert.h>
size_t my_strlen(const char* str)
{
assert(str);
int tmp = 0;
while (*str)
{
tmp++;
str++;
}
if (!tmp)
return NULL;
return tmp;
}
二、模拟实现strcpy

char* my_strcpy(char* strDestination, const char* strSource)
{
assert(strDestination&& strSource);
char* tmp_strDestination = strDestination;
while (*strDestination++ = *strSource++);
return tmp_strDestination;
}
三、模拟实现strcat

#include<stdio.h>
#include<assert.h>
char* my_strcat(char* strDestination, const char* strSource)
{
assert(strDestination&& strSource);
char* tmp_strDestination = strDestination;
while (*strDestination++);
strDestination--;
while (*strDestination++ = *strSource++);
return tmp_strDestination;
}
四、模拟实现strcmp

#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* string1, const char* string2)
{
assert(string1 && string2);
for (;;string1++, string2++)
{
if (*string1 == *string2 && *string2 == '\0')
return 0;
else if (*string1 == '\0' || *string1 < *string2)
return -1;
else if(*string2 == '\0' || *string1 > *string2)
return 1;
}
}
五、模拟实现strstr

#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* string, const char* strCharSet)
{
assert(string && strCharSet);
char* tmp_string = (char*)string;
do
if (*tmp_string == *strCharSet)
return tmp_string;
while (*tmp_string++);
return NULL;
}
六、模拟实现memcpy

#include<stdio.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t count)
{
assert(dest && src);
char* tmp_dest = dest;
while (count--)
*((char*)dest)++ = *((char*)src)++;
return tmp_dest;
}
七、模拟实现memmove

#include<stdio.h>
#include<assert.h>
void* my_memmove(void* dest, const void* src, size_t count)
{
assert(dest && src);
void* tmp_dest = dest;
if (dest < src)
while (count--)
*((char*)dest)++ = *((char*)src)++;
else
while (count--)
*((char*)dest + count) = *((char*)src + count);
return dest;
}
七、看完记得给个三连,谢谢
边栏推荐
- Beyond Compare 4 试用期到期
- R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化密度图、使用stat_central_tendency函数在密度中添加均值竖线并自定义线条类型
- (ES6以上以及TS) Map对象转数组
- [CLion] CLion always prompts "This file does not belong to any project target xxx" solution
- 英特尔全方位打造算力基础,助推“算”赋百业
- 如何将第三方服务中心注册集成到 Istio ?
- leetcode/submatrix element sum
- 2022 Go生态圈 rpc 框架 Benchmark
- 找出相同属性值的对象 累加数量 汇总
- win10系统重装,无法登录进行同步的情况下chrome数据恢复
猜你喜欢

重庆市大力实施智能建造,推动建筑业数字化转型,助力“建造强市”

用户体验 | 如何度量用户体验 ?

Promise learning (4) The ultimate solution for asynchronous programming async + await: write asynchronous code in a synchronous way

【随心笔记】假期快过去了,都干了点什么

收藏|机械工程师面试常问问题

Apex installation error

达梦更换正式授权dm.key

.NET analyzes the LINQ framework in depth (three: the elegant prelude of LINQ)
![[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement](/img/fc/cd859efa69fa7b45f173de74c04858.png)
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement

Process sibling data into tree data
随机推荐
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
浏览器存储
Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
pandas connects to the oracle database and pulls the data in the table into the dataframe, filters all the data from the current time (sysdate) to one hour ago (filters the range data of one hour)
leetcode/子矩阵元素和
R语言两个时间序列数据的滞后相关性可视化:使用forecast包的ccf函数绘制交叉相关函数,根据可视化结果分析滞后相关性
实现集中式身份认证管理的案例
C#/VB.NET 将PPT或PPTX转换为图像
Aeraki Mesh 加入 CNCF 云原生全景图
初级必备:单例模式的7个问题
R language ggplot2 visualization: use the ggdensity function of the ggpubr package to visualize density plots, use the stat_central_tendency function to add mean vertical lines to the density and cust
[Nodejs] fs module of node
Dameng replaces the officially authorized dm.key
博弈论(Depu)与孙子兵法(42/100)
Data frame and remote frame of CAN communication
蔚来又一新品牌披露:产品价格低于20万
[Open class preview]: Research and application of super-resolution technology in the field of video quality enhancement
【倒计时5天】探索音画质量提升背后的秘密,千元大礼等你来拿
Beyond Compare 4 试用期到期
win10系统重装,无法登录进行同步的情况下chrome数据恢复













