当前位置:网站首页>Implementation of StrCmp, strstr, memcpy, memmove
Implementation of StrCmp, strstr, memcpy, memmove
2022-07-28 13:57:00 【Wang Honghua x】
One 、strcmp
1、 Definition
int strcmp(const char* str1, const char* str2);2、 Function parsing
This function compares the first character of two strings . If they are equal , Just continue to compare the next pair , Until the characters are different or encounter '\0' .str1 and str2 Are two strings to be compared , If the first mismatched character is str1 Small in the middle , Return to one <0 Value , If in str1 Hit in , Return to one >0 Value , If the contents of two strings are equal, it returns 0 .
3、 Function realization

4、 Code testing

Two 、strstr
1、 Definition
const char* strstr(const char* str1, const char* str2);2、 Function parsing
This function will return a pointer , Point to str2 For the first time in str1 Where in , If str2 No str1 Part of , Just return a null pointer . The matching process will not include '\0' , But it will be there. '\0' Stop at .
3、 Function realization
#include <stdio.h>
#include <assert.h>
const char* MyStrstr(const char* str1, const char* str2)
{
assert(str1 && str2);
// If str1 End found , Just close the loop
while (*str1)
{ // Find the first pair of matching characters
while (*str1 != *str2 && *str1 != '\0')
str1++;
int i = 0;
// At this time, the first pair of equal characters appear , Continue to match
while (*(str1 + i))
{
// Because the first pair of characters have been matched equal , therefore i You can start with ++
i++;
// If str2 Yes '\0', prove str1 There is str2 The content of
if (*(str2 + i) == '\0')
return str1;
// If in '\0' Before matching to unequal , Just make the next match
if (*(str1 + i) != *(str2 + i))
break;
}
// If str1 + i yes '\0', signify str1 It is impossible to include str2 了
if (*(str1 + i))
str1++;
else
return NULL;
}
if (*str2 == '\0')
return str1;
else
return NULL;
}
4、 Code testing

3、 ... and 、memcpy
1、 Definition
void* memcpy(void* destination, const void* source, size_t num);2、 Function parsing
This function will num A value of bytes from source Copy the location pointed to destination Point to memory block ;source Pointers and destination The underlying type of the object pointed to by the pointer is independent of the function itself , The result is a binary copy of the data ; The function will not be in source Detect any terminating null characters in , It will accurately copy num Bytes ; To avoid crossing the border ,destination and source The size of the array pointed to by the parameter , It should be at least num Bytes , And will not overlap .( For overlapping blocks of memory ,memmove Is a safer function .)
destination Returned .
3、 Function realization

4、 Code testing

Four 、memmove
1、 Definition
void* memmove(void* destination, const void* source, size_t num);2、 Function parsing
Before parsing the function of this function , Let's do one thing with the last function ;
Obviously ,memcpy Function can't do , The reason is simple , When you want to ‘c’ When copying ,'c' The original content and by 'a' To cover , So it's impossible to copy . therefore , For overlapping blocks of memory ,memcpy Appear unsafe .
Now let's see memmove function , This function can realize the movement of memory blocks . from sourc Point to the location of the copy num A value of bytes to destination Point to memory block . It seems that an intermediate buffer is used for copying , allow destination and source overlap .
source Pointers and destination The underlying type of the object pointed to by the pointer is independent of the function itself , The result is a binary copy of the data ; The function will not be in source Detect any terminating null characters in , It will accurately copy num Bytes ; To avoid crossing the border ,destination and source The size of the array pointed to by the parameter , It should be at least num Bytes ;
destination Returned .
3、 Function realization
This function is implemented , It's very simple , Just open up a space and copy a copy of the original data . But we can try to open up as little space as possible .

When source The position pointed to is destination In front of , Let's start with the last data 5 Start copying , You can see that before copying , The data to be copied will not be overwritten

And when source The position pointed to is destination In the back of , From the first data 5 Start copying , This effect can also be achieved .

4、 Code testing
边栏推荐
- 多线程与高并发(三)—— 源码解析 AQS 原理
- Understanding of stack and practical application scenarios
- 算法---不同路径(Kotlin)
- 要想组建敏捷团队,这些方法不可少
- 安全保障基于软件全生命周期-NetworkPolicy应用
- R language uses LM function to build linear regression model and subset function to specify subset of data set to build regression model (use floor function and length function to select the former pa
- Denial of service DDoS Attacks
- 7.依赖注入
- Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open
- 【架构】评分较高的三本微服务书籍的阅读笔记
猜你喜欢

Customized template in wechat applet

30天刷题计划(三)

对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解

《机器学习》(周志华) 第6章 支持向量 学习心得 笔记

DDoS protection with iptables

Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development

111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误

30天刷题计划(二)

30 day question brushing plan (II)

DXF reading and writing: align the calculation of the position of the dimension text in the middle and above
随机推荐
.net for subtraction, intersection and union of complex type sets
Rolling update strategy of deployment.
Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
使用 IPtables 进行 DDoS 保护
不用Swagger,那我用啥?
《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
R语言使用lm函数构建线性回归模型、使用subset函数指定对于数据集的子集构建回归模型(使用floor函数和length函数选择数据前部分构建回归模型)
数据库系统原理与应用教程(061)—— MySQL 练习题:操作题 21-31(五)
Qt5开发从入门到精通——第一篇概述
Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
Rust from introduction to mastery 01 introduction
数据库系统原理与应用教程(060)—— MySQL 练习题:操作题 11-20(四)
The strongest distributed locking tool: redisson
数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
Poj3259 wormhole solution
Socket class understanding and learning about TCP character stream programming
Operator3-设计一个operator
strcmp、strstr、memcpy、memmove的实现
Dojnoip201708 cheese solution
To build agile teams, these methods are indispensable