当前位置:网站首页>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
边栏推荐
- 社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
- 1216_MISRA_C规范学习笔记_控制流的规则要求
- The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
- Hierarchical output binary tree
- 简述存储器的分级策略
- RAID0和RAID5的创建和模拟RAID5工作原理
- Feign source code analysis
- [go]常见的并发模型[泛型版]
- 每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
- Summary of facial classics
猜你喜欢
数据库之Mysql——概述安装篇
Cloud computing skills competition -- the first part of openstack private cloud environment
Attack and defense world misc questions 1-50
Go语学习笔记 - 数组 | 从零开始Go语言
Go语学习笔记 - 异常处理 | 从零开始Go语言
Ctf-misc summary
三星,再次“西征”
upload-labs 靶场练习
Samsung, March to the west again
Go语学习笔记 - Slice、Map | 从零开始Go语言
随机推荐
[Effective Go 中文翻译]函数篇
浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda
C语言学习记录——삼십팔 字符串函数使用和剖析(2)
高精度焊接机械臂定位
NIH降血脂指南《your guide to lowering your Cholesterol with TLC》笔记(持续更新中)
【无标题】
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
php生成短链接:将数字转成字母,将字母转成数字
浅谈ES6尾调优化
使用 Ingress 实现金丝雀发布
Mobile terminal layout (3D conversion, animation)
编译原理题-带答案
NFT ecological development of Ignis public chain: unicorn Donation and development of Art
Quick rehearsal exercise
thinkphp6+jwt 实现登录验证
数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲
几种智能机器人室内定位方法对比
Samsung, March to the west again
Comparison of indoor positioning methods of several intelligent robots
[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface