当前位置:网站首页>字符串复制、拼接、比较以及分割函数总结(一)
字符串复制、拼接、比较以及分割函数总结(一)
2022-07-30 16:42:00 【BSP初级小学僧】
博客主页:https://blog.csdn.net/weixin_46094737?type=blog
欢迎评论留言 如有错误敬请指正!
本文由小学生廉原创,首发于 CSDN
未来很长,值得我们全力奔赴更美好的生活!
注意:在使用字符串处理函数时,一定要使用 #include <string.h> 开头
1.strcpy()函数
原型声明:char strcpy(char dest, const char *src);
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
#include <stdio.h>
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;//在str02字符后面增加字符'a',并且再循环增加3个字符
str02[i+22]='\0';//在字符后面增加一个字符串结束标志'\0'
puts(str02);
return 0;
}运行结果:

2、字符串拼接函数
其形式为:strcat(字符数组1,字符数组2)——字符串连接的函数
作用是把两个字符数组中的字符串连接起来,把字符串2连接到字符串1的后面,结果放在字符数组1中。
#include <stdio.h>
int main()
{
char str01[100]="strcat test! ";
char str02[100]="str02 to str01";
puts(str01);
puts(str02);
strcat(str01,str02);//将str01的内容追加到str02后面
puts(str01);
return 0;
}运行结果:

3、字符串比较函数
其形式为:strcmp(字符数组1,字符串2)——字符串比较的函数
因为字符串不能用等号来比较大小,所以就用strcmp函数来比较!!!!!!
比较规则:
(1)如果全部字符相同,则认为两个字符串相等;
(2)若出现不相同的字符,则以第一对不相同的字符比较结果为准。('a'<'z';'A'<'Z')。
比较的结果由函数值带回:
(3)如果字符串1与字符串2相同,则函数值为0。
#include <stdio.h>
int main()
{
/*
判定标准:当s1和s2字符串相等时,返回0
s1>s2,当s1中字符第一次出现大于s2字符
反之亦然
*/
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;
}运行结果:

4、字符串切割函数
函数原型:char *strtok(char *s, char *delim);
函数功能:把字符串s按照字符串delim进行分割,然后返回分割的结果。
函数使用说:
1.strtok函数的实质上的处理是,strtok在s中查找包含在delim中的字符并用NULL(’/0′)来替换,直到找遍整个字符串。这句话有两层含义:(1)每次调用strtok函数只能获得一个分割单位。(2)要获得所有的分割单元必须反复调用strtok函数。
2.strtok函数以后的调用时的需用NULL来替换s.
3.形参s(要分割的字符串)对应的变量应用char s[]=”….”形式,而不能用char *s=”….”形式。
#include <stdio.h>
int main()
{
char s1[]="thappy new year!";
char *p=NULL;
printf("分割之前的字符:");
puts(s1);
printf("分割之后的字符:");
p=strtok(s1,"a");
puts(p);
return 0;
}运行结果:

边栏推荐
猜你喜欢

Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs

云风:不加班、不炫技,把复杂的问题简单化

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

Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)

Paper reading (63): Get To The Point: Summarization with Pointer-Generator Networks

Qt 容器控件之Tab Widget 使用详解

打印1-100之间的奇数

实现web实时消息推送的7种方案

Discuz杂志/新闻报道模板(jeavi_line)UTF8-GBK模板

SMI 与 Gateway API 的 GAMMA 倡议意味着什么?
随机推荐
Rounding out the most practical way of several DLL injection
华为云WeLink携手伙伴,共建协同办公生态
全职做自媒体靠谱吗?
第一次用debug查询,发现这个为空,是不是代表还没获得数据库的意思?求帮助。
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
HUAWEI CLOUD data governance production line DataArts, let "data 'wisdom' speak"
Public Key Retrieval is not allowed error solution
静态网页和动态网页的不同之处;该如何选择服务器呢
huato hot update environment construction (DLL method hot update C# code)
在 Chrome 浏览器中安装 JSON 显示插件
Google Cloud Spanner的实践经验
How does the new retail saas applet explore the way to break the digital store?
Discuz magazine/news report template (jeavi_line) UTF8-GBK template
Security business revenue growth rate exceeds 70% 360 builds digital security leader
3D激光SLAM:LeGO-LOAM论文解读---实验对比
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
(一)云计算技术学习--虚拟化vSphere学习
onenote使用
Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs
C语言学习之旅 【函数(二)】