当前位置:网站首页>Deep parsing strings and memory functions
Deep parsing strings and memory functions
2022-07-21 04:50:00 【Finally become master Kangkang】
Kangkang recently learned many string functions and memory functions , Among them, the basic function Kangkang doesn't say much . I want to tell you something strstr、strtok、strerror、memmove、memmset、memcmp.
strstr Function is used to find substrings . It is used as follows :
char*strstr(constchar*string,constchar*strCharSet); Its first argument points to a large string , The second parameter points to a small string ( The string being searched ) Its return value is the address of the large string . Now let's try it !
So how to simulate and realize its functions ?
char* my_strstr(const char* des, const char* src)
{
assert(des && src);
char* s1 = des;
char* s2 = src;
char* p = des;
while (*p != '\0')
{
while (*s1 != *s2)
{
p++;
s1 = p;
}
while (s1 != '\0' && s2 != '\0' && *s1 == *s2);
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return p;
}
p++;
s2 = src;
}
return NULL;
}
strtok The use of functions is very strange , Its usage rules are as follows :
char*strtok(char*str,constchar*sep );
1.sep The parameter is a string , Defines the set of characters used as separators
The first parameter specifies a string , It contains 0 One or more by sep A character separated by one or more separators in a string
remember .
2.strtok Function found str The next mark in , And use it \0 ending , Returns a pointer to the tag .( notes :
strtok Function changes the string being manipulated , So it's using strtok The string segmented by function is usually the content of temporary copy
And it can be modified .)
3.strtok The first argument of the function is not NULL , Function will find str The first mark in ,strtok The function will save it in the string
Position in .
strtok The first argument to the function is NULL , The function will start at the same position in the string that is saved , Find the next target
remember .
4. If there are no more tags in the string , Then return to NULL The pointer .
Now let me give you an example :
strerror Function is used to return the error information corresponding to the error code . The following functions are used :
char*strerror(interrnum);
Now let's run it !!
Next, let's talk about the use of character functions , First of all memmove.
String functions are only for strings , The memory function is for memory .
void*memmove(void*dest,constvoid*src,size_tcount);
Now let me give you an example :
So how to simulate it ? The code is as follows :
Simulation Implementation memmove
#include <stdio.h>
#include <assert.h>
void* my_memmove(void* dest, const void* src, size_t num)
{
void* ret = dest;
assert(dest && src);
if (dest < src)
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
memset How to use the function ?
memset Is the setting of function memory , The following functions are used :
void*memset(void*dest,intc,size_tcount);
Here is how the code works :
This is the setting of memory data !
The last function is memcmp, It's very easy to use .
It is a byte by byte comparison ,intmemcmp(constvoid*buf1,constvoid*buf2,size_tcount);
When buf1>buf2 When , Return a greater than 0 The number of .
When buf1=buf2 When , return 0.
When buf1<buf2 When , Returns a number less than zero . The following is an example for everyone :
These are my views on these functions , If there is any wrong , I hope you can correct me !
边栏推荐
- MySQL数据库并发,上锁的问题(共享锁和排它锁)
- Access数据库对象包括哪六个?Access与 Excel 最重要的区别是什么?
- SCS [1] today starts a single-cell journey, describing the past and present lives of single-cell sequencing
- DNA 9. 揭秘肿瘤异质性与TMB, MSI之间的相关性
- 通俗解释: IaaS,PaaS和SaaS的区别
- 關於XML 編輯工具
- 定义一个描述圆的类,然后生成一个圆对象,最后输出半径、直径和面积。
- JS基础--Math
- Static routing - Comprehensive Experiment
- 欲戴王冠,必承其重。
猜你喜欢
最新UPX3.91-支持win64/PE-加/脱壳
Configuration of static routes to achieve network wide accessibility
Extract the characters in the list in comma separated form
JS基础--JSON
[leetcode] 150 evaluation of inverse Polish expression
Dynamic routing protocol OSPF comprehensive experiment
DNA 9. Uncover the correlation between tumor heterogeneity and TMB, MSI
Installation and management procedures
RNA 20. SCI 文章中单样本免疫浸润分析 (ssGSEA)
DNA 10. 识别癌症驱动基因 (OncodriveCLUST)
随机推荐
GDB installation process problems
RNA 24. SCI文章中基于TCGA的免疫浸润细胞分析的在线小工具——TIMER
Configuration of static routes to achieve network wide accessibility
JS基础--Math
c语言文件操作管理(下)
LVM and disk quota
NAT network address translation
【云原生之kubernetes】kubernetes集群下初始化容器的使用方法
IF:14+ “冒烟型”骨髓瘤的分子组成突显了导致多发性骨髓瘤的进化途径
与传统IT开发相比,低代码平台有何优势?
Three methods: arrange strings in reverse order (instead of printing in reverse order)
Hibernate method to prevent SQL injection attack
Apktool回编译问题解决方案总结
MySQL数据库并发,上锁的问题(共享锁和排它锁)
FigDraw 13. SCI 文章绘图之桑葚图及文章复现(Sankey)
四个问题,判断自己是否适合学习编程
FigDraw 15. SCI 文章绘图之多组学圈图(OmicCircos)
[FPGA tutorial case 31] communication case 1 - ask modulation signal generation based on FPGA
JS面试题--ES5和ES6有什么区别?
DNA 11. Identify mutation hotspots on the three-dimensional structure of tumor proteins (hotspot3d)