当前位置:网站首页>库函数的模拟实现(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;
}
七、看完记得给个三连,谢谢
边栏推荐
- Envoy source code flow chart
- Online - GCeasy GC log analysis tools
- How to use DevExpress controls to draw flowcharts?After reading this article, you will understand!
- 一文带你彻底厘清 Isito 中的证书工作机制
- 收藏|机械工程师面试常问问题
- Visualization of lag correlation of two time series data in R language: use the ccf function of the forecast package to draw the cross-correlation function, and analyze the lag correlation according t
- Pytest电商项目实战(下)
- JS数据类型转换完全攻略
- Sparse representation - study notes
- bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)
猜你喜欢

这项工作事关中小学生生命安全!五部门作出联合部署

将同级数据处理成树形数据

Programmer's self-cultivation

CloudCompare&PCL ICP配准(点到面)

2022 Go ecosystem rpc framework Benchmark

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

语音聊天app源码——语音聊天派对

Qt get all files in a folder
![[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

稀疏表示--学习笔记
随机推荐
这项工作事关中小学生生命安全!五部门作出联合部署
Excel表格打印时不打印标记填充颜色
Jenkins安装插件遇到的问题
《MySQL核心知识》第6章:查询语句
bpmn-process-designer基础上进行自定义样式(工具、元素、菜单)
浏览器存储
[Cloud Enjoying Freshness] Community Weekly Vol.73- DTSE Tech Talk: 1 hour in-depth interpretation of SaaS application system design
R language fitting ARIMA model: use the auto.arima function in the forecast package to automatically search for the best parameter combination, model order (p, d, q), set the seasonal parameter to spe
mysql进阶(二十二)MySQL错误之Incorrect string value中文字符输入错误问题分析
(ES6以上以及TS) Map对象转数组
R语言两个时间序列数据的滞后相关性可视化:使用forecast包的ccf函数绘制交叉相关函数,根据可视化结果分析滞后相关性
C#/VB.NET 将PPT或PPTX转换为图像
那些利用假期学习的职场人,后来都怎么样了?
The use of Ts - Map type
STM32 CAN filter configuration details
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
数字化转型实践:世界级2B数字化营销的方法框架
bat countdown code
Process sibling data into tree data
CloudCompare & PCL ICP registration (point to face)













