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

边栏推荐
- Nervegrowold d2l (7) kaggle housing forecast model, numerical stability and the initialization and activation function
- 数据库课程设计大作业大盘点【建议在校生收藏】
- Login Module Debugging - Getting Started with Software Debugging
- .NET 6.0中使用Identity框架实现JWT身份认证与授权
- MySQL 8.0.29 解压版安装教程(亲测有效)
- Wanhua chemical fine chemical industry innovation product assembly
- node.js中怎么连接redis?
- 升级Win11后不喜欢怎么退回Win10系统?
- 【SOC FPGA】Peripheral KEY LED
- 3D激光SLAM:LeGO-LOAM论文解读---系统概述部分
猜你喜欢

理解实现搜索二叉树

swagger使用教程——快速使用swagger
![[TypeScript] Introduction, Development Environment Construction, Basic Types](/img/d7/b3175ab538906ac1b658a9f361ba44.png)
[TypeScript] Introduction, Development Environment Construction, Basic Types

Nervegrowold d2l (7) kaggle housing forecast model, numerical stability and the initialization and activation function

huato hot update environment construction (DLL method hot update C# code)

(1) Cloud computing technology learning - virtualized vSphere learning

升级Win11后不喜欢怎么退回Win10系统?

Leetcode 118. 杨辉三角

服务器装好系统的电脑怎么分区
![[TypeScript]简介、开发环境搭建、基本类型](/img/d7/b3175ab538906ac1b658a9f361ba44.png)
[TypeScript]简介、开发环境搭建、基本类型
随机推荐
理解实现搜索二叉树
【Linux操作系统】 虚拟文件系统 | 文件缓存
第一次用debug查询,发现这个为空,是不是代表还没获得数据库的意思?求帮助。
Discuz magazine/news report template (jeavi_line) UTF8-GBK template
第六章:决胜秋招
LeetCode318:单词长度的最大乘积
疫情之下的裁员浪潮,7点建议帮你斩获心仪offer
PyQt5快速开发与实战 9.2 数据库处理
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
李沐d2l(七)kaggle房价预测+数值稳定性+模型初始化和激活函数
Login Module Debugging - Getting Started with Software Debugging
3D激光SLAM:LeGO-LOAM论文解读---激光雷达里程计与建图
Go新项目-编译热加载使用和对比,让开发更自由(3)
[MRCTF2020]Ezaudit
论文阅读 (63):Get To The Point: Summarization with Pointer-Generator Networks
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
测试管理与规范
2022-07-30 Androd 进入深度休眠后把WIFI给关掉,唤醒之后重新打开WIFI
You are a first-class loser, you become a first-class winner
@Bean注解详解