当前位置:网站首页>strcat()、strcpy()、strcmp()、strlen()
strcat()、strcpy()、strcmp()、strlen()
2022-04-23 06:30:00 【五五六六0524】
const修饰的变量或对象的值不变,保护变量或对象的内容不会在被调用期间修改
1、字符串连接函数strcat
函数原型为strcat(char[],const char[])
第2个字符数组被声明为const,以保证数组中的内容不会在函数被调用期间被修改
那么,为什么不把第1个字符数组也声明为const呢?
为了使第2个数组中的内容覆盖第一个数组末尾的'\0',字符串是否结束的标志是'\0'的位置,如果一个数组中包含一个以上'\0',则遇到第一个'\0'时就结束
#include <iostream>
using namespace std;
int main() {
char a[30]="i love ";//要保证数组长度足够容纳两者两加
char b[] = "china";
cout << strcat(a, b)<< endl;
return 0;
}
2、字符串复制函数strcpy
函数原型strcpy(char[],const char[])
第2个字符数组不改变,将第1个字符数组中相应字符覆盖
#include <iostream>
using namespace std;
int main() {
char a[100];
char b[] = "i love china";
strcpy(a, b);
cout << a<< endl;
return 0;
}
3、字符串比较函数strcmp
函数原型strcmp(const char[],const char[])
目的是比较这两个字符串,因此都应该是只比较而不改变
前面的字符串>后面的字符串,值为正数,前面的<后面的,值为负数,前面的=后面的,值为0
#include <iostream>
using namespace std;
int main() {
char a[] = "i love china";
char b[] = "i love";
cout << strcmp(a,b) << endl;
return 0;
}
4、字符串长度函数strlen
函数原型strlen(const chat[])
实际长度,不包括'\0',应用中可能会报错说:没有为字符串添加0终止符,初始化一下字符串即可,如char str[100]={0}
#include <iostream>
using namespace std;
int main() {
char a[] = "i love china";
cout << strlen(a) << endl;
return 0;
}
版权声明
本文为[五五六六0524]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wow0524/article/details/121976561
边栏推荐
- 一些关于网络安全的好教程或笔记的链接,记录一下
- Post of experience in preparation for guarantee and research -- the 18th (2021) Central South planning department promoted the exemption to Zhejiang University Institute of Technology
- Weblux file upload and download
- 【Unity VFX】VFX特效入门笔记-火花制作
- Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
- linux下mysql数据库备份与恢复(全量+增量)
- 如何展示您的数字作品集:来自创意招聘人员的建议
- 内网渗透系列:内网隧道之pingtunnel
- Série de pénétration Intranet: icmpsh du tunnel Intranet
- Simplify exporting to SVG data files and all images in SVG folder
猜你喜欢
How to present your digital portfolio: suggestions from creative recruiters
内网渗透系列:内网隧道之icmptunnel(DhavalKapil师傅的)
内网渗透系列:内网隧道之icmp_tran
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
Houdini流体>>粒子流体导出到unity笔记
如何展示您的数字作品集:来自创意招聘人员的建议
内网渗透系列:内网隧道之icmptunnel(jamesbarlow师傅的)
MySQL in window10 version does not work after setting remote access permission
第七章 资产减值
VBA calls SAP RFC to read & write data
随机推荐
【Unity VFX】VFX特效入门笔记-火花制作
每天工作4小时的程序员
Internal network security attack and defense: a practical guide to penetration testing (5): analysis and defense of horizontal movement in the domain
Zhuang understand's TA notes (VI) < fakeenvreflect & rust, rust effect >
SAP Query增强开发介绍
SAP STO With Billing流程与配置
MySQL in window10 version does not work after setting remote access permission
Houdini > variable building roads, learning process notes
Intranet security attack and defense: a practical guide to penetration testing (6): domain controller security
One of event management
SAP自建表log功能开启
Nodejs (VI) sub process operation
《内网安全攻防:渗透测试实战指南》读书笔记(四):权限提升分析及防御
Unity C# 单例模式 学习复习笔记
内网渗透系列:内网隧道之dns2tcp
【NLP笔记】CRF原理初探
Houdini地形与流体解算(模拟泥石流)
Dictionary & lt; T1,T2&gt; Sorting problem
Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
Unity ugui determines the solution of clicking on the UI and 3D objects