当前位置:网站首页>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
边栏推荐
- Nodejs (VI) sub process operation
- Protobuf 使用
- 内网渗透系列:内网隧道之icmptunnel(jamesbarlow师傅的)
- 三分钟教你用Houdini流体>>解算粒子流体水滴
- SAP TR手动导入系统操作手册
- 一文了解系列,对web渗透的常见漏洞总结(持续更新)
- MySQL8. 0 installation / uninstallation tutorial [window10 version]
- Unity ugui determines the solution of clicking on the UI and 3D objects
- Internal network security attack and defense: a practical guide to penetration testing (8): Authority maintenance analysis and defense
- Apache Hudi 如何加速传统的批处理模式?
猜你喜欢
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
[unity VFX] Introduction notes of VFX special effects - spark production
Dropping Pixels for Adversarial Robustness
SQL sorts string numbers
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
How to present your digital portfolio: suggestions from creative recruiters
Protobuf 使用
内网渗透系列:内网隧道之pingtunnel
Houdini > rigid body, rigid body breaking RBD
【NLP笔记】CRF原理初探
随机推荐
Export all SVG files in the specified path into pictures in PNG format (thumbnail or original size)
Enterprise wechat login free jump self built application
TA notes of Zhuang understand (VII) < Lambert + Phong + shadow + 3evcolor + Ao >
Unity C# 单例模式 学习复习笔记
KCD_EXCEL_OLE_TO_INT_CONVERT报错SY-subrc = 2
Houdini terrain and fluid solution (simulated debris flow)
Unity get real geographic map application terrain notes
VBA调用SAP RFC实现数据读取&写入
Towords Open World Object Detection
《内网安全攻防:渗透测试实战指南》读书笔记(五):域内横向移动分析及防御
C SVG path parser of xamarin version
[NLP notes] preliminary study on CRF principle
STO With Billing 跨公司库存转储退货
Unity 获取一个文件依赖的资源
Idea shortcut
[unity VFX] Introduction notes of VFX special effects - spark production
Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
Zhuang understand's TA notes (VI) < fakeenvreflect & rust, rust effect >
Dropping Pixels for Adversarial Robustness
《内网安全攻防:渗透测试实战指南》读书笔记(七):跨域攻击分析及防御