当前位置:网站首页>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:

边栏推荐
- huato hot update environment construction (DLL method hot update C# code)
- onenote use
- 基于STM32F407使用ADC采集电压实验
- Deep Feedback Network for Recommendation
- 说几个大厂分库分表的那点破事。
- 如何注册域名、备案以及解析
- [HarekazeCTF2019]Avatar Uploader 1
- Goland opens file saving and automatically formats
- Large-scale integrated office management system source code (OA+HR+CRM) source code sharing for free
- 第一次用debug查询,发现这个为空,是不是代表还没获得数据库的意思?求帮助。
猜你喜欢
随机推荐
Deep Feedback Network for Recommendation
Dive deep on Netflix‘s recommender system(Netflix推荐系统是如何实现的?)
3D激光SLAM:LeGO-LOAM论文解读---特征提取部分
SocialFi 何以成就 Web3 去中心化社交未来
华为云数据治理生产线DataArts,让“数据‘慧’说话”
Lotus 1.16.0 minimum snapshot export import
FP6600QSO SOP-8 USB专用充电端口控制器 用于快充电协议和QC2.0/3.0
你是一流的输家,你因此成为一流的赢家
Oracle动态监听与静态监听详解
真正懂经营管理的CIO具备哪些特质
获得抖音商品详情 API
onenote使用
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
MySql统计函数COUNT详解
【Linux操作系统】 虚拟文件系统 | 文件缓存
OpenCV形状检测
代码越写越乱?那是因为你没用责任链
leetcode:1488. 避免洪水泛滥【二分 + 贪心】
[TypeScript] Introduction, Development Environment Construction, Basic Types
MySQL详细学习教程(建议收藏)




![[MRCTF2020]Ezaudit](/img/80/d4656abdff20703591ffdc3f5a5ebc.png)




