当前位置:网站首页>库函数的模拟实现(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;
}
七、看完记得给个三连,谢谢
边栏推荐
- 如何成功通过 CKA 考试?
- Aeraki Mesh 加入 CNCF 云原生全景图
- 【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
- MNIST是什么(plist是什么意思)
- R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用xscale函数指定X轴坐标轴度量调整方式、设置x轴坐标为scientific使用科学计数法显示坐标值
- Detailed explanation of table join
- Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器
- The use of Ts - Map type
- C#/VB.NET 将PPT或PPTX转换为图像
- How to get the address of WeChat video account (link address of WeChat public account)
猜你喜欢

Jenkins安装插件遇到的问题

程序员如何优雅地解决线上问题?

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

博弈论(Depu)与孙子兵法(42/100)

Online - GCeasy GC log analysis tools

蔚来又一新品牌披露:产品价格低于20万

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

【倒计时5天】探索音画质量提升背后的秘密,千元大礼等你来拿

Find objects with the same property value Cumulative number Summarize

并发编程10大坑,你踩过几个?
随机推荐
千万级乘客排队系统重构&压测方案——总结篇
Aeraki Mesh 加入 CNCF 云原生全景图
[5 days countdown] to explore the secret behind the great quality promotion, gift waiting for you to take of $one thousand
Aeraki Mesh 正式成为 CNCF 沙箱项目
重庆市大力实施智能建造,推动建筑业数字化转型,助力“建造强市”
Dameng replaces the officially authorized dm.key
Flutter Widget 如何启用和屏蔽点击事件
基于ArkUI eTS开发的坚果食谱(NutRecipes)
Detailed explanation of table join
Istio Meetup China:全栈服务网格 - Aeraki 助你在 Istio 服务网格中管理任何七层流量
如何成功通过 CKA 考试?
一文带你彻底厘清 Isito 中的证书工作机制
Promise learning (4) The ultimate solution for asynchronous programming async + await: write asynchronous code in a synchronous way
Istio Meetup China: Full Stack Service Mesh - Aeraki Helps You Manage Any Layer 7 Traffic in an Istio Service Mesh
leetcode每日一题:字符串压缩
OpenHarmony高校技术俱乐部计划发布
How to Integrate Your Service Registry with Istio?
JS数据类型转换完全攻略
CloudCompare & PCL ICP registration (point to face)
Pytest e-commerce project combat (below)













