当前位置:网站首页>库函数的模拟实现(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;
}
七、看完记得给个三连,谢谢
边栏推荐
- JS数据类型转换完全攻略
- 蔚来又一新品牌披露:产品价格低于20万
- MNIST是什么(plist是什么意思)
- 基于ArkUI eTS开发的坚果食谱(NutRecipes)
- 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
- leetcode/子矩阵元素和
- pgAdmin 4 v6.12 发布,PostgreSQL 开源图形化管理工具
- Feign 从注册到调用原理分析
- 【面试高频题】难度 1.5/5,二分经典运用题
- 2022 Go ecosystem rpc framework Benchmark
猜你喜欢
随机推荐
Process sibling data into tree data
Several methods of appending elements are commonly used in js: append, appendTo, after, before, insertAfter, insertBefore, appendChild
win10系统重装,无法登录进行同步的情况下chrome数据恢复
(ES6以上以及TS) Map对象转数组
Meshlab & Open3D SOR filtering
《MySQL核心知识》第6章:查询语句
MNIST是什么(plist是什么意思)
英特尔全方位打造算力基础,助推“算”赋百业
R语言拟合ARIMA模型:使用forecast包中的auto.arima函数自动搜索最佳参数组合、模型阶数(p,d,q)、设置seasonal参数指定在模型中是否包含季节信息
Programmer's self-cultivation
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
js中常用追加元素的几种方法:append,appendTo,after,before,insertAfter,insertBefore,appendChild
ACL 2022 | 文本生成的相关前沿进展
STM32 CAN filter configuration details
Data frame and remote frame of CAN communication
Find objects with the same property value Cumulative number Summarize
Fault 007: The dexp derivative is inexplicably interrupted
表连接详解
Jenkins安装插件遇到的问题
【讲座分享】“营收“看金融






















