当前位置:网站首页>深度剖析 string —— strcmp & strncmp
深度剖析 string —— strcmp & strncmp
2022-07-21 05:04:00 【十里坡小白】
系列文章目录
深度剖析:strcat & strncat
深度剖析:strchr & strstr
深度剖析:strcmp & strncmp
深度剖析:strcpy & strncpy
深度剖析:strlen & strtok
深度剖析:memcpy & memmove
深度剖析:memset & memcmp
前言
库函数string是我们经常使用也是非常重要的代码,我将会对常用的函数的用法和实现原理进行深度剖析
一、strcmp
1.调用结构
int strcmp(const char *str1, const char *str2);
2.使用详解
strcmp:把 str1 所指向的字符串和 str2 所指向的字符串进行比较
原理详解:
- 比较ASCII码值
- 第一个字符串 > 第二个字符串,则返回 大于0 的数字
- 第一个字符串 = 第二个字符串,则返回 0
- 第一个字符串 < 第二个字符串,则返回 小于0 的数字
3.源码剖析
int Strcmp(char const *s1, char const *s2) {
//比较:把 str1 所指向的字符串和 str2 所指向的字符串进行比较。
assert(s1 && s2); //防止传入空指针
while (*s1 == *s2 && *s1 != 0 && *s2 != 0) {
//比较字符串
s1++;
s2++;
}
return *s1 - *s2; //返回比较结果
}
二、strncmp
1.调用结构
int strncmp(const char *str1, const char *str2, size_t n);
2.使用详解
strncmp:把 str1 和 str2 进行比较,最多比较前 n 个字节
原理详解:
- 比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完
- 第一个字符串 > 第二个字符串,则返回 大于0 的数字
- 第一个字符串 = 第二个字符串,则返回 0
- 第一个字符串 < 第二个字符串,则返回 小于0 的数字
3.源码剖析
int Strncmp(char const *s1, char const *s2, int len) {
//比较:把 str1 和 str2 进行比较,最多比较前 n 个字节
assert(s1 && s2); //防止传入空指针
while (len-- && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
//比较 n 个字节的字符串
s1++;
s2++;
}
return *s1 - *s2; //返回比较结果
}
边栏推荐
- OneNote plug-in, cloud expansion
- Loopback input and loopback output
- Remove the spaces in the file names in batches and replace the spaces in the data stored in the database
- Pre training and fine tuning of Google Bert model for beginners
- Pycharm Professional Edition creates flask project | downloads flask package | and some examples
- UNET reproduction and environment configuration (including dataset)
- 使用MATLAB App Design 工具设计一个 简易App
- AS7341光谱传感器测量色温color_temperature_学习笔记
- (environment configuration) TDD net
- 猫狗图片资源
猜你喜欢
OLED(经典0.96英寸)--4SPI--SSD1306控制原理(含常用芯片_oled例程)
DataLossError : corrupted record at XXXXXXX,BERT预训练报错
荣耀手机冰箱app激活
Amy-Tabb机器人世界手眼标定(2、实验结果)
【PCB】基于合泰HT32F52352芯片电路板绘制实验(WiFi及光传感模块)-画板笔记
【记录】Optisystem运行卡死,无法点击关闭、输入变量数值等问题解决方法
合泰32-Onenet-WiFi模块-合泰单片机通过MQTT协议数据上云(二)
PyTorch基础模块和实践
Mask RCNN loading weight error
深度剖析 —— 数据
随机推荐
图的邻接表及其深度优先(DFS)、广度优先(BFS)遍历
Shortcut keys, commands
Account类
2. Learn the vector calculation of paddlepaddle from scratch
BERT模型调用源码分析
Owncloud 9.0 better cross server sharing and scalability
深度学习源码项目里的checkpoint
FTP cannot create multi-level directories [circular creation problem]
Self attention principle
Amy-Tabb机器人世界手眼标定(2、实验结果)
ESP8266固件下载及烧录(收录AT固件下载地址+固件烧录注意事项)
ES聚合统计语法
Amy-Tabb机器人世界手眼标定(1、环境搭配)
El radio value cannot be echoed
Mmdetection environment matching (cuda10.1+mmdet2.24)
Yum install GCC error
荣耀手机冰箱app激活
基于OpenCV和Dlib+fr的人脸检测以及基于Dlib人脸对齐
cv2的相关函数
3. Build the basic model of paddlepaddle from scratch (compare with keras and pytorch)