当前位置:网站首页>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);
}
边栏推荐
- WiFi 5GHz frequency
- Dare to go out for an interview without learning some distributed technology?
- office_ Delete the last page of word (the seemingly blank page)
- IDEA xml中sql没提示,且方言设置没用。
- LeetCode-归并排序链表
- Message mechanism -- message processing
- BGP experiment the next day
- 【毕业季·进击的技术er】年少有梦,何惧彷徨
- Actual combat | use composite material 3 in application
- Free drawing software recommended - draw io
猜你喜欢
Federal learning: dividing non IID samples according to Dirichlet distribution
Target free or target specific: a simple and effective zero sample position detection comparative learning method
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Several methods of capturing packets under CS framework
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
[C language] basic learning notes
Why can't you remember when reading? Why can't you remember- My technology learning methodology
CorelDRAW Graphics Suite2022免费图形设计软件
How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
随机推荐
Shutdown procedure after 60
Realize the function of data uploading
缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
Wechat applet calculates the distance between the two places
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
okcc为什么云呼叫中心比传统呼叫中心更好?
Landing guide for "prohibit using select * as query field list"
Thinkphp6 limit interface access frequency
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
C - derived classes and constructors
Mysql表insert中文变?号的问题解决办法
C language practice - number guessing game
The solution to the complexity brought by lambda expression
Ognl和EL表达式以及内存马的安全研究
The core idea of performance optimization, dry goods sharing
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
第十六周作业
Deep understanding of lambda expressions
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Learn what definitelytyped is through the typescript development environment of SAP ui5