当前位置:网站首页>Summary of String Copy, Concatenation, Comparison and Split Functions (1)
Summary of String Copy, Concatenation, Comparison and Split Functions (1)
2022-07-30 16:55:00 【BSP Junior Primary School Monk】
Blog homepage: https://blog.csdn.net/weixin_46094737?type=blog
Comments are welcome If there are any mistakes, please correct me!
This article was originally written by the primary school student Lian, first published on CSDN
The future is very long, and it is worth our efforts to strive for a better life!
Note: When using string processing functions, be sure to start with #include
1.strcpy() function
Prototype declaration: char strcpy(char dest, const char *src);
Function: put string is copied to the address space starting with dest
Note: The memory areas pointed to by src and dest cannot overlap and dest must haveenough space to hold the string of src.
Returns a pointer to dest.
#include int main(){char str01[50]="happy birthday to you";char str02[50];char *p=NULL;p=strcpy(str02,str01);puts(p);int i;str02[i+21]=' ';for(i=0;i<4;i++)str02[i+22]='a'+i;//Add the character 'a' after the str02 character, and add 3 characters in a loopstr02[i+22]='\0';//Add a string end mark '\0' after the characterputs(str02);return 0;} Running result:

2. String concatenation function
The form is: strcat(character array 1, character array 2) - a function of string concatenation
The effect is to put the String concatenate, concatenate string 2 to the back of string 1, and place the result in character array 1.
#include int main(){char str01[100]="strcat test! ";char str02[100]="str02 to str01";puts(str01);puts(str02);strcat(str01, str02);//Append the contents of str01 to str02puts(str01);return 0;} Run result:

3, string comparison function
The form is: strcmp(character array 1, string 2) - a function of string comparison
Because strings cannot use the equal sign to compare the size, use the strcmp function to compare!!!!!!
Comparison rules:
(1) If all characters are the same, the two strings are considered equal;
(2) If there are different characters, the comparison result of the first pair of different characters shall prevail.('a' < 'z'; 'A' < 'Z').
The result of the comparison is brought back by the function value:
(3) If string 1 is the same as string 2, the function value is 0.
#include int main(){/*Judgment criteria: When the strings of s1 and s2 are equal, return 0s1>s2, when the first occurrence of a character in s1 is greater than the character in s2vice versa*/char s1[]="happy new day!";char s2[]="happy new day!";char s3[]="hbppy new day!";char s4[]="aappy new day!";int ret=-1;ret = strcmp(s1,s2);printf("%d\n",ret);ret = strcmp(s1,s3);printf("%d\n",ret);ret = strcmp(s1,s4);printf("%d",ret);return 0;} Run result:

4. String cutting function
Function prototype: char *strtok(char *s, char *delim);
Function function: split the string s according to the string delim, and then return the split result.
Function usage says:
1. The essence of the strtok function is, strtok finds the characters contained in delim in s and replaces it with NULL('/0'), until the whole string is searched.This sentence has two meanings: (1) Each time the strtok function is called, only one division unit can be obtained.(2) To obtain all the division units, the strtok function must be called repeatedly.
2. Subsequent calls to the strtok function need to replace s with NULL.
3. The variable corresponding to the formal parameter s (string to be split) should be in the form of char s[]=”….”, but not in the form of char *s=”….”.
#include int main(){char s1[]="thappy new year!";char *p=NULL;printf("Character before split: ");puts(s1);printf("Character after splitting: ");p=strtok(s1,"a");puts(p);return 0;} Running result:

边栏推荐
- You are a first-class loser, you become a first-class winner
- Is it reliable to work full-time in self-media?
- DTSE Tech Talk丨Phase 2: 1 hour in-depth interpretation of SaaS application system design
- 数据库的三大范式
- SwiftUI SQLite教程之带有历史的搜索栏List App (教程含完整代码)
- 优酷视频元素内容召回系统:多级多模态引擎探索
- Deep Feedback Network for Recommendation
- Wanhua chemical fine chemical industry innovation product assembly
- The way of life, share with you!
- 如何在 UE4 中用代码去控制角色移动
猜你喜欢

华为云数据治理生产线DataArts,让“数据‘慧’说话”

23. Please talk about the difference between IO synchronization, asynchronous, blocking and non-blocking

Security business revenue growth rate exceeds 70% 360 builds digital security leader

JVM学习----垃圾回收

哎,这要人老命的缓存一致问题啊

安全业务收入增速超70% 三六零筑牢数字安全龙头

DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计

Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs

《痞子衡嵌入式半月刊》 第 59 期

Public Key Retrieval is not allowed error solution
随机推荐
从零开始的Multi-armed Bandit
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
Gorilla Mux 和 GORM 的使用方法
李沐d2l(七)kaggle房价预测+数值稳定性+模型初始化和激活函数
Chapter 6: Decisive Autumn Moves
为人处世之道,与君共勉!
你是一流的输家,你因此成为一流的赢家
[极客大挑战 2020]Roamphp1-Welcome
牛客网刷题——运算符问题
Express框架连接MySQL及ORM框架
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
华为云数据治理生产线DataArts,让“数据‘慧’说话”
【SOC FPGA】外设KEY点LED
DTSE Tech Talk丨Phase 2: 1 hour in-depth interpretation of SaaS application system design
Rounding out the most practical way of several DLL injection
You are a first-class loser, you become a first-class winner
新零售saas小程序如何探索数字化门店的破局之路?
[TypeScript]简介、开发环境搭建、基本类型
vivo announced to extend the product warranty period, the system launched a variety of functional services