当前位置:网站首页>(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,如果我的文章能对您有帮助的话,麻烦各位观众姥爷们点赞、收藏、关注,你们的每一次举手之劳都将化为船长的前进动力!
边栏推荐
- LeetCode刷题之第55题
- SharedPreferences and SQlite database
- 十、视图解析原理与源码分析
- 最简单的防抖节流理解法
- 【数据库和SQL学习笔记】10.(T-SQL语言)函数、存储过程、触发器
- PoE视频监控解决方案
- 网络信息安全运营方法论 (上)
- CVPR2020 - 自校准卷积
- 沁恒MCU从EVT中提取文件建立MounRiver独立工程
- [Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
猜你喜欢
随机推荐
dataframe 常用操作
常见的 PoE 错误和解决方案
记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
AIDL详解
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
【ts】typescript高阶:分布式条件类型
【Pytorch学习笔记】11.取Dataset的子集、给Dataset打乱顺序的方法(使用Subset、random_split)
四、Web场景之静态资源配置原理
【shell编程】第三章:函数
Tensorflow steps on the pit notes and records various errors and solutions
[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)
【22李宏毅机器学习】课程大纲概述
沁恒MCU从EVT中提取文件建立MounRiver独立工程
MSRA提出学习实例和分布式视觉表示的极端掩蔽模型ExtreMA
LeetCode刷题之第701题
CVPR2021 - Inception Convolution with Efficient Dilation Search
单片机按键开发库-支持连击、长按等操作
Redis集群(docker版)——从原理到实战超详细
伪RTOS-ProroThread在CH573芯片上的移植
LeetCode刷题之第530题









