当前位置:网站首页>Summary of common string processing functions in C language
Summary of common string processing functions in C language
2022-07-02 04:34:00 【small_ planet】
1、char *strcat(char *s1, const char *s2)
Splicing string function , hold s2 Joining together to s1 After the string , The return value points to s1 The pointer to
int main()
{
char s1[] = "aba";
char s2[] = "cdf";
printf(" Before splicing s1 length :%d s2 length : %d\n", strlen(s1), strlen(s2));
char *s3=strcat(s1, s2);
//s3 Equate to s1
printf(" Print s1: %s\n", s1);
printf(" Print s3: %s\n", s3);
printf(" After splicing s1 length :%d s2 length : %d s3 length :%d\n", strlen(s1), strlen(s2),strlen(s3));
}
Be careful :s2 Joining together to s1 Back ,s1 The length of will change from the original 3 Turn into 6( No end character ), Here the program does not report an error because C Language does not detect array subscript out of bounds , So it's using strcat() Function, try to give enough memory space , In addition to calculating the number of characters after splicing, you need to add 1 Just enough space to store the empty characters at the end . So at the beginning of the definition s1 When it comes to length, it is best to consider the length after splicing , The initial length here is set to :s1 The length of +s2 The length of + Terminator =3+3+1=7, Although the program may not report an error after buffer overflow , But for the safety of the program, it's better to be rigorous .
2、char *strstr(const char *s1, const char *s2)
lookup s2 First appeared in s1 The location of , return s2 For the first time in s1 The first address that appears , Returns if none exists NULL
int main()
{
//s2 stay s1 We can find
char s1[] = "acdrtcdrj";
char s2[] = "cdr";
char *s3 = strstr(s1, s2);
printf("%s\n", s3);
//str2 stay str1 Cannot be found in
char str1[] = "acdrtcdrj";
char str2[] = "cdrx";
char *str3 = strstr(str1, str2);
printf("%s\n", str3);
}
边栏推荐
- Feature Engineering: summary of common feature transformation methods
- Wechat applet map annotation
- 10 minutes to understand CMS garbage collector in JVM
- FAQ | FAQ for building applications for large screen devices
- LeetCode-对链表进行插入排序
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
- Play with concurrency: draw a thread state transition diagram
- How to solve the problem that objects cannot be deleted in Editor Mode
- Mysql database learning
猜你喜欢
One click generation and conversion of markdown directory to word format
社交媒体搜索引擎优化及其重要性
Learn AI safety monitoring project from zero [attach detailed code]
What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
BGP experiment the next day
Landing guide for "prohibit using select * as query field list"
[C language] basic learning notes
记录一次Unity 2020.3.31f1的bug
Actual combat | use composite material 3 in application
The confusion I encountered when learning stm32
随机推荐
Free drawing software recommended - draw io
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Mysql表insert中文变?号的问题解决办法
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
[JS -- map string]
UNET deployment based on deepstream
60后关机程序
Research on the security of ognl and El expressions and memory horse
Pytorch---使用Pytorch进行图像定位
Dare to go out for an interview without learning some distributed technology?
Www 2022 | rethinking the knowledge map completion of graph convolution network
C language practice - number guessing game
cs架构下抓包的几种方法
Mysql中常见的锁
Leetcode- insert and sort the linked list
I sorted out some basic questions about opencv AI kit.
云服务器的安全设置常识
Federal learning: dividing non IID samples according to Dirichlet distribution
Pytorch-Yolov5從0運行Bug解决:
Markdown编辑语法