当前位置:网站首页>Deep analysis of string -- StrCmp & strncmp
Deep analysis of string -- StrCmp & strncmp
2022-07-21 20:28:00 【Shilipo Xiaobai】
List of articles
Depth analysis :strcat & strncat
Depth analysis :strchr & strstr
Depth analysis :strcmp & strncmp
Depth analysis :strcpy & strncpy
Depth analysis :strlen & strtok
Depth analysis :memcpy & memmove
Depth analysis :memset & memcmp
List of articles
Preface
Library function string It is also very important code that we often use , I will deeply analyze the usage and implementation principle of commonly used functions
One 、strcmp
1. Call structure
int strcmp(const char *str1, const char *str2);
2. The use of,
strcmp: hold str1 The string pointed to and str2 Compare the string pointed to
The principle, :
- Compare ASCII Code value
- First string > Second string , Then return to Greater than 0 The number of
- First string = Second string , Then return to 0
- First string < Second string , Then return to Less than 0 The number of
3. Source analysis
int Strcmp(char const *s1, char const *s2) {
// Compare : hold str1 The string pointed to and str2 Compare the string pointed to .
assert(s1 && s2); // Prevent passing null pointers
while (*s1 == *s2 && *s1 != 0 && *s2 != 0) {
// Compare strings
s1++;
s2++;
}
return *s1 - *s2; // Return the comparison result
}
Two 、strncmp
1. Call structure
int strncmp(const char *str1, const char *str2, size_t n);
2. The use of,
strncmp: hold str1 and str2 Compare , Up to before comparison n Bytes
The principle, :
- Compare to a different character or the end of a string or num Compare all the characters
- First string > Second string , Then return to Greater than 0 The number of
- First string = Second string , Then return to 0
- First string < Second string , Then return to Less than 0 The number of
3. Source analysis
int Strncmp(char const *s1, char const *s2, int len) {
// Compare : hold str1 and str2 Compare , Up to before comparison n Bytes
assert(s1 && s2); // Prevent passing null pointers
while (len-- && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
// Compare n A byte string
s1++;
s2++;
}
return *s1 - *s2; // Return the comparison result
}
边栏推荐
- 1020月饼
- Deep analysis of string -- memset & memcmp
- 深度剖析 string —— strcpy & strncpy
- Stm32f407 based camera (ov7670 without FIFO) image acquisition and LCD display experiment - note sorting
- 1024 scientific counting method
- (笔记)吴恩达深度学习L4卷积神经网络W1
- 12.【I/O流get()与getline()与put()函数】
- 深度剖析 string —— memset & memcmp
- boost::this_thread::sleep (boost::posix_time::microseconds (100000))报错“this_thread”:“::”左侧的符号必须是一种类型
- 力扣中 26.删除有序数组中的重复项 88.合并两个有序数组 和189.旋转数组
猜你喜欢
Pytorch Basics
1027打印沙漏
Let the code solve the series of "linear algebra" - find the greatest common factor of polynomials
scanf 详解 - 你所不知道的scanf用法
三子棋
Owncloud 9.0 better cross server sharing and scalability
让代码解决《线性代数》系列 - 多项式求和
使用Arduino搭建基于阿里云平台的物联网智能家居
Hetai 32 onenet WiFi module - Hetai MCU data cloud through mqtt protocol (II)
今天不拉扯了 不拉扯了 碎碎念 上
随机推荐
Face detection based on OpenCV and dlib+fr and face alignment based on Dlib
理解JS的三座大山
Stm32f407 based camera (ov7670 without FIFO) image acquisition and LCD display experiment - note sorting
(environment configuration) TDD net
矩阵求导常用公式(避坑)+矩阵的模和矩阵的绝对值的求导
4. 10 lines of code MNIST handwritten numeral recognition of paddlepaddle
Checkpoint in the deep learning source code project
无人机的微分平坦性详细推导
Semantic segmentation, instance segmentation
Examples of enumeration
字符函数和字符串函数及其模拟实现(C语言)
Hetai 32 onenet WiFi module - Hetai MCU data cloud through mqtt protocol (II)
Bert from introduction to practice notebook
(好数对)一个数组的值赋给另一个数组
(剑指off版)旋转数组求最小值(通俗易懂)
Pytorch installation
1019数字黑洞
深度剖析 string —— strcat & strncat
Amy-Tabb机器人世界手眼标定(2、实验结果)
PCL学习第九章《采样一致性》