当前位置:网站首页>C language learning record -- use and analysis of string function (2)
C language learning record -- use and analysis of string function (2)
2022-04-23 08:12:00 【kongqizyd146】
strcmp
int strcmp(const char* string1, const char* string2)
1 The string is greater than 2 character string , Return is greater than the 0 The number of
1 The string is less than 2 character string , Back to less than 0 The number of
Equal return 0
#include <stdio.h>
int main()
{
char* p1 = "abcdef";
char* p2 = "qwert";
int ret = strcmp(p1, p2);
printf("%d\n", ret);
return 0;
}
The method of comparison is not the ratio of the number of elements to the size , But the corresponding element ratio ANSCII Code value , such as a and q Than ,a Less than q, So back -1.
But this is vs What's going on inside , Different compilation environments return different values , But the scope is the same , Greater than 0, Less than 0, be equal to 0.
Simulation Implementation
#include <stdio.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 = *str2)
{
if (*str1 == '\0')
{
return 0;
}
str1++;
str2++;
}
if (*str1 > *str2)
return 1;
else(*str1 < *str2)
return -1;
}
int main()
{
char* p1 = "abcdef";
char* p2 = "abqwe";
int ret = my_strcmp(p1, p2);
printf("ret = %d\n", ret);
return 0;
}
If abc and abcd Compare , Then cycle to \0 and d Compare , Dissimilarity , Just come to the following if else,0 Less than d, return -1.
perhaps if else Change to return (*str1 - *str2) Can also be .
Functions with unlimited length :strcpy,strcat,strcmp. Read \0 Just stop , But it's not safe , such as strcpy function , If the destination is not enough, prevent the source string , But it was copied . So it's not safe . There are also length limited functions :strncpy,strncat,strncmp.
strncpy
char* strncpy(char* strDest, const char* strSource, size_t count)
count That is to copy a few bytes , Unit is byte . such as
int main()
{
char arr1[10] = "abcdef";
char arr2[] = "hello bit";
strncpy(arr1, arr2, 4);
return 0;
}
Copy 4 Bytes ,\0 It won't be copied . When the specified number is greater than the size of the source string , The rest will be made up \0, Until the specified number of bytes
About this function , That's how it's defined
#include <stdio.h>
char* _cdecl strncpy(char* dest, const char* source, size_t count)
{
char* start = dest;
while (count && (*dest++ = *source++)) /* copy string*/
count--;
if (count) /* pad out with zeroes*/
while (--count)
*dest++ = '\0';
return(start)
}
alike , The other two are also based on the original plus the number of bytes
Append function strncat, After addition , It will automatically add one last \0, Because it will eventually be appended to a string . If count Greater than the number of strings appended in the past , Then after the addition, put a \0, The addition is over , There won't be several in the back \0
Definition
char* _cdecl strncat(char* front, const char* back, size_t count)
{
char* start = front;
while (*front++)
front--;
while (count--)
if (!(*front++ = *back++))
return(start);
*front = '\0';
return(start);
}
strncmp
That is to compare several characters , The value is different .
strstr
char* strstr(const char*, const char*)
int main()
{
char* p1 = "abcdef";
char* p2 = "def";
char* ret = strstr(p1, p2);
if (ret == NULL)
{
printf(" Substring does not exist \n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
If the function cannot be found, it will return a null pointer , If found, it will return d The address of , Using pointer variables ret After receiving ,hiu from d Start printing back , If at this time p1 yes abcdefgh, Then the result will come out abcdefgh. If p1 yes abcdefabcdef, There are two def, Then the result will start from the first .
Simulation Implementation
#include <stdio.h>
#include <assert.h>
char* my_strstr(const char* p1, const char* p2)
{
assert(p1 != NULL);
assert(p2 != NULL);
char* s1 = p1;
char* s2 = p2;
char* cur = (char*)p1;
if (*p2 = '\0')
{
return p1;
}
while (*cur)
{
s1 = cur;
s2 = (char*)p2;
while ( (*s1 != '\0') && (*s2 != '\0') && (*s1 == *s2) )
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return cur;
}
cur++;
}
return NULL;
}
int main()
{
char* p1 = "abcdefgh";
char* p2 = "def";
char* ret = strstr(p1, p2);
if (ret == NULL)
{
printf(" Substring does not exist \n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
You'd better draw a picture to understand . Library functions are defined as
char* my_strstr(const char* str1, const char* str2)
{
char* cp = str1;
char* s1, * s2;
if (!*str2)
return((char*)str1);
while (*cp)
{
s1 = cp;
s2 = (char*)str2;
while (s1 && s2 && !(*s1 - *s2))
s1++, s2++;
if (!*s2)
return(cp);
cp++;
}
return (NULL);
}
In fact, it can also be optimized based on the first one . stay s2 Of if Write this after the statement
if (*s1 == '\0')
{
return NULL;
}
end .
版权声明
本文为[kongqizyd146]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230652011329.html
边栏推荐
- Ctf-misc learning from start to give up
- Intranet penetration series: dns2tcp of Intranet tunnel
- Go语学习笔记 - 异常处理 | 从零开始Go语言
- 简述存储器的分级策略
- 云计算技能大赛 -- openstack私有云环境 第二部分
- 求3个字符串(每串不超过20个字符)中的最大者。
- Essays (updated from time to time)
- Solidity IDE Remix中文版使用手册
- Research on system and software security (2)
- MySQL -- the secret of lock -- how to lock data
猜你喜欢

社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播

Concours de compétences en informatique en nuage - - première partie de l'environnement cloud privé openstack

Go语学习笔记 - 异常处理 | 从零开始Go语言

Guoji Beisheng openstack container cloud environment construction

如何在SQL Server中导入excel数据,2019版

浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda

yum源仓库本地搭建的两种方法
![[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface](/img/f1/09de53509479a01098d3cf46bf48eb.jpg)
[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface
![[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP](/img/6c/7408180d0c24560b4a68982635520e.jpg)
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP

云计算技能大赛 -- openstack私有云环境 第一部分
随机推荐
浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda
数据库之MySQL——基础篇
Research on software security based on NLP (2)
Intranet penetration series: dns2tcp of Intranet tunnel
Talking about distributed storage from ES, mongodb, redis and rocketmq
Research on system and software security (5)
Guoji Beisheng openstack container cloud environment construction
Asynchronous learning
LeetCode 1611. 使整数变为 0 的最少操作次数
云计算技能大赛 -- openstack私有云环境 第一部分
AAAI 2022招募讲者啦!!
访问数据库的时候出现错误 Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.详解
KVM安装部署
Cloud computing skills competition -- the first part of openstack private cloud environment
Flatten arrays
DVWA靶场练习
Discussion on ES6 tail tune optimization
为什么会存在1px问题?怎么解决?
Ctf-misc summary
C outputs a two-dimensional array with the following characteristics.