当前位置:网站首页>(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,如果我的文章能对您有帮助的话,麻烦各位观众姥爷们点赞、收藏、关注,你们的每一次举手之劳都将化为船长的前进动力!
边栏推荐
- 《基于机器视觉测量系统的工业在线检测研究》论文笔记
- 【Pytorch学习笔记】11.取Dataset的子集、给Dataset打乱顺序的方法(使用Subset、random_split)
- 对象比较
- Comparison and summary of Tensorflow2 and Pytorch in terms of basic operations of tensor Tensor
- [Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)
- 表情捕捉的指标/图像的无参考质量评价
- CVPR best paper winner Huang Gao's team from Tsinghua University presented the first dynamic network review
- Service
- [Database and SQL study notes] 9. (T-SQL language) Define variables, advanced queries, process control (conditions, loops, etc.)
- 多边形等分
猜你喜欢

【ts】typescript高阶:模版字面量类型

哥廷根大学提出CLIPSeg,能同时作三个分割任务的模型

SQL (2) - join window function view

神经网络也能像人类利用外围视觉一样观察图像

2021电赛资源及经验总结

读论文 - Unpaired Portrait Drawing Generation via Asymmetric Cycle Mapping

【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法

【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)

最简单的防抖节流理解法

OSPF网络类型
随机推荐
MSRA proposes extreme masking model ExtreMA for learning instances and distributed visual representations
【论文精读】ROC和PR曲线的关系(The relationship between Precision-Recall and ROC curves)
Comparison and summary of Tensorflow2 and Pytorch in terms of basic operations of tensor Tensor
【数据库和SQL学习笔记】9.(T-SQL语言)定义变量、高级查询、流程控制(条件、循环等)
【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera
【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)
【ts】typescript高阶:键值类型及type与interface区别
单片机按键开发库-支持连击、长按等操作
网络信息安全运营方法论 (下)
十、视图解析原理与源码分析
LeetCode刷题之第74题
教你如何封装功能组件和页面组件
tensorflow的session和内存溢出
AIDL详解
九、响应处理——内容协商底层原理
深度学习系列(二)优化器 (Optimization)
【Pytorch学习笔记】10.如何快速创建一个自己的Dataset数据集对象(继承Dataset类并重写对应方法)
MaskDistill-不需要标注数据的语义分割
SQL(1) - Add, delete, modify and search
Jupyter notebook选择不同的Anaconda环境作为内核运行