当前位置:网站首页>(C语言)strlen、strcpy、strcat、strcmp、strstr函数的模拟实现
(C语言)strlen、strcpy、strcat、strcmp、strstr函数的模拟实现
2022-08-05 05:17:00 【Captain林】
本篇文章计划用简单的代码方式向大家介绍部分内存函数的模拟实现
主要目录如下
目录
1.模拟实现strlen-测字符串长度
//模拟实现strlen-测字符串长度
#include<stdio.h>
#include<assert.h>
size_t my_strlen(const char* str)
{
assert(str != NULL);//不用解引用,null要大写
int count = 0;
while (*str)
{
if (*str != "\0")
{
count++;
str++;
}
}
return count;
}
int main()
{
char arr[] = "abcdef";
int ret = my_strlen(arr);
printf("%d", ret);
return 0;
}2.模拟实现strcpy - 复制字符串
//模拟实现strcpy - 复制字符串
//strcpy函数返回值可以实现链式访问
#include<stdio.h>
#include<assert.h>
char* my_strcpy(char* arr1, const char* arr2)//返回的是地址,char*
{
/*assert(*arr1 && *arr2);*/
assert(arr1 && arr2);//不用解引用
char* ret = arr1;
while (*arr1++ = *arr2++)//因为这里涉及到改变指针所指向的值,因此要解引用;要用=!!!
{
;
}
return ret;//要返回地址,但不是arr1移动之后的地址
}
int main()
{
//char* arr1 = "abcdef";//要有数组的大小!
char arr1[20] = "abcdef";//要有足够空间
//char arr2[3] = "abc";
char* arr2 = "abc";//字符串应该用指针
printf("%s", my_strcpy(arr1, arr2));
return 0;
}3.模拟实现strcat - 拷贝链接
//模拟实现strcat - 拷贝链接
#include<stdio.h>
#include<assert.h>
char* my_strcat(char* arr1, const char* arr2)
{
assert(arr1 && arr2);
char* ret = arr1;
//找到\0
while (*arr1)
{
arr1++;
}
//链接
while (*arr1++ = *arr2++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "abc";
char arr2[20] = "def";
//返回值是地址
printf("%s\n", my_strcat(arr1, arr2));
return 0;
}
4.模拟实现strcmp - 对比
//模拟实现strcmp - 对比
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* arr1, const char* arr2)
{
while (*arr1 == *arr2)//比对
{
//为0
if (*arr1 == '\0')
{
return 0;
}
arr1++;
arr2++;
}
//不相等
return (int)*arr1 - (int)*arr2;
}
int main()
{
char arr1[] = "abcd";
char arr2[] = "abcf";
int ret = my_strcmp(arr1, arr2);
if (ret > 0)
{
printf(">\n");
}
else if (ret = 0)
{
printf("=\n");
}
else
{
printf("<");
}
return 0;
}5.模拟实现strstr - 寻找字符串子集
//模拟实现strstr - 寻找字符串子集
#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* arr1, const char* arr2)
{
assert(arr1 && arr2);
const char* s1 = arr1;
const char* s2 = arr2;
char* cur = arr1;
while (*cur)
{
s1 = cur;
s2 = arr2;
while (*s1 && *s2 && (*s1 == *s2))
{
s1++;
s2++;
}
if (*s2 == '\0')//这里需要单引号,且不能放在循环中
{
return cur;
}
cur++;
}
return NULL;//s1已经到\0
}
int main()
{
char arr1[20] = "abcdef";
char arr2[20] = "bcd";
//字串?
char* ret = my_strstr(arr1, arr2);
if (NULL != ret)
{
printf("%s\n", ret);
}
else
{
printf("找不到子集");
}
return 0;
}6.结语
看到这里,相信老铁们对如何应用这些内存函数已经有了基本的了解。我是计算机海洋的新进船长Captain_ldx,如果我的文章能对您有帮助的话,麻烦各位观众姥爷们点赞、收藏、关注,你们的每一次举手之劳都将化为船长的前进动力!
边栏推荐
- A deep learning code base for Xiaobai, one line of code implements 30+ attention mechanisms.
- 九、响应处理——内容协商底层原理
- dataframe 常用操作
- 【ts】typescript高阶:条件类型与infer
- [Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
- 网络信息安全运营方法论 (下)
- 记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
- MySQL
- LeetCode刷题之第74题
- PID详解
猜你喜欢

十一、拦截器运行原理

基于STM32F407的WIFI通信(使用的是ESP8266模块)

【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)

HuiFer 带你读懂 BeanFactory getBean 方法
![[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed](/img/ef/a058ec08bd0a6313e3610a4ebc9685.png)
[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed

【nodejs】第一章:nodejs架构

【数据库和SQL学习笔记】6.SELECT查询4:嵌套查询、对查询结果进行操作

三、自动配置源码分析

LeetCode刷题之第24题

【Shell编程】第一章:子串
随机推荐
【Shell编程】第一章:子串
It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)
MySQL主从复制—有手就能学会的MySQL集群搭建教程
网工必用神器:网络排查工具MTR
面向小白的深度学习代码库,一行代码实现30+中attention机制。
LeetCode刷题之第1024题
Redis集群(docker版)——从原理到实战超详细
单变量线性回归
LeetCode刷题之第530题
LeetCode刷题之第23题
AIDL detailed explanation
LeetCode刷题之第33题
【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
dataframe 常用操作
教你如何封装功能组件和页面组件
Redis设计与实现(第二部分):单机数据库的实现
[Database and SQL study notes] 10. (T-SQL language) functions, stored procedures, triggers
MySQL