当前位置:网站首页>库函数的模拟实现(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;
}
七、看完记得给个三连,谢谢
边栏推荐
- 【likeshop】回收租凭系统100%开源无加密 商城+回收+租赁
- leetcode/子矩阵元素和
- 并发编程10大坑,你踩过几个?
- 初级必备:单例模式的7个问题
- 每日一题:连续子数组的最大和(动态规划)
- Fault 007: The dexp derivative is inexplicably interrupted
- Complete Raiders of JS Data Type Conversion
- CAN通信的数据帧和远程帧
- 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
- Envoy 源码流程图
猜你喜欢
随机推荐
OpenHarmony高校技术俱乐部计划发布
The CAN communication standard frame and extended frame is introduced
[Unity3D Plugin] AVPro Video Plugin Share "Video Player Plugin"
【随心笔记】假期快过去了,都干了点什么
MVVM响应式
【倒计时5天】探索音画质量提升背后的秘密,千元大礼等你来拿
The difference between undefined and null in JS
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化密度图、使用stat_central_tendency函数在密度中添加均值竖线并自定义线条类型
[Nodejs] fs module of node
【面试高频题】难度 1.5/5,二分经典运用题
A new generation of ultra-safe cellular batteries, Sihao Airun goes on sale starting at 139,900 yuan
小程序插件如何帮助开发者受益?
【likeshop】回收租凭系统100%开源无加密 商城+回收+租赁
故障007:dexp导数莫名中断
ACL 2022 | 文本生成的相关前沿进展
数字化转型实践:世界级2B数字化营销的方法框架
安装apex报错
Istio Meetup China:全栈服务网格 - Aeraki 助你在 Istio 服务网格中管理任何七层流量
JS数据类型转换完全攻略
R language ggplot2 visualization: use ggpubr package ggscatter function visualization scatterplot, use xscale wasn't entirely specified X axis measurement adjustment function, set the X coordinate for






















