当前位置:网站首页>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);
} 
边栏推荐
- Keil compilation code of CY7C68013A
- Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
- 记录一次Unity 2020.3.31f1的bug
- MySQL advanced SQL statement 2
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
- [C language] Dynamic Planning --- from entry to standing up
- Message mechanism -- message processing
- Recyclerview add header
- Shutdown procedure after 60
- Pytoch --- use pytoch for image positioning
猜你喜欢

Idea autoguide package and autodelete package Settings

Li Kou interview question 02.08 Loop detection

Sword finger offer II 006 Sort the sum of two numbers in the array

MySQL error: expression 1 of select list is not in group by claim and contains nonaggre

cs架构下抓包的几种方法

The confusion I encountered when learning stm32

Www2022 | know your way back: self training method of graph neural network under distribution and migration

MySQL advanced SQL statement 2

Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects

Play with concurrency: what's the use of interruptedexception?
随机推荐
Wechat applet pull-down loading more waterfall flow loading
Alibaba cloud polkit pkexec local rights lifting vulnerability
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
C - derived classes and constructors
win11安装pytorch-gpu遇到的坑
Homework of the 16th week
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
Dare to go out for an interview without learning some distributed technology?
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
Design and implementation of general interface open platform - (44) log processing of API services
Yyds dry inventory compiler and compiler tools
Okcc why is cloud call center better than traditional call center?
Introduction to vmware workstation and vSphere
社交媒体搜索引擎优化及其重要性
How much is the tuition fee of SCM training class? How long is the study time?
Markdown edit syntax
What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
Actual combat | use composite material 3 in application
LxC limits the number of CPUs
C language practice - number guessing game