当前位置:网站首页>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
边栏推荐
- Unable to process jar entry [module info. Class]
- 05数组的使用
- Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
- 如何展示您的数字作品集:来自创意招聘人员的建议
- SQL sorts string numbers
- Intranet security attack and defense: a practical guide to penetration testing (6): domain controller security
- [unity VFX] Introduction notes of VFX special effects - spark production
- Internal network security attack and defense: a practical guide to penetration testing (IV): Authority improvement analysis and defense
- C # control the camera, rotate and drag the observation script (similar to scenes observation mode)
- Houdini terrain and fluid solution (simulated debris flow)
猜你喜欢

VBA調用SAP RFC實現數據讀取&寫入

VBA appelle SAP RFC pour réaliser la lecture et l'écriture des données

【Unity VFX】VFX特效入门笔记-火花制作

Houdini > fluid, rigid body export, learning process notes

Dropping Pixels for Adversarial Robustness

VBA调用SAP RFC实现数据读取&写入

C problem of marking the position of polygons surrounded by multiple rectangles

Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly

SAP STO With Billing流程与配置

About USB flash drive data prompt raw, need to format, data recovery notes
随机推荐
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
How does Apache Hudi accelerate traditional batch mode?
Internal network security attack and defense: a practical guide to penetration testing (8): Authority maintenance analysis and defense
C#控制相机,旋转,拖拽观察脚本(类似Scenes观察方式)
内网渗透系列:内网隧道之icmp_tran
Nodejs (four) character reading
读取修改resource文件夹下的json文件
C reads the registry
Simplify exporting to SVG data files and all images in SVG folder
Dropping Pixels for Adversarial Robustness
Idea shortcut
Houdini > fluid, rigid body export, learning process notes
Unity ugui determines the solution of clicking on the UI and 3D objects
How to present your digital portfolio: suggestions from creative recruiters
Weblux file upload and download
常用Markdown语法学习
SAP STO With Billing流程与配置
TA notes of Zhuang understand (VII) < Lambert + Phong + shadow + 3evcolor + Ao >
《内网安全攻防:渗透测试实战指南》读书笔记(八):权限维持分析及防御
Unity gets a resource that is referenced by those resources