当前位置:网站首页>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
边栏推荐
- Unity gets a resource that is referenced by those resources
- 一些靶场的学习记录:sqli-labs、upload-labs、XSS
- 常用Markdown语法学习
- C # control the camera, rotate and drag the observation script (similar to scenes observation mode)
- C reads the registry
- 《内网安全攻防:渗透测试实战指南》读书笔记(六):域控制器安全
- Use of command line parameter passing library argparse
- Automatically fit single line text into the target rectangle
- 平面定义-平面方程
- Unity screen adaptation
猜你喜欢

C # control the camera, rotate and drag the observation script (similar to scenes observation mode)

Intranet penetration series: icmptunnel of Intranet tunnel (by master dhavalkapil)

SAP TR手动导入系统操作手册

Export all SVG files in the specified path into pictures in PNG format (thumbnail or original size)

内网渗透系列:内网隧道之icmptunnel(DhavalKapil师傅的)

The projection vector of a vector to a plane

企业微信免登录跳转自建应用

CTF-MISC总结

Unity C single case mode learning review notes

Towords Open World Object Detection
随机推荐
C problem of marking the position of polygons surrounded by multiple rectangles
Weblux file upload and download
命令行参数传递库argparse的使用
C#操作注册表全攻略
如何展示您的数字作品集:来自创意招聘人员的建议
Houdini > variable building roads, learning process notes
爬虫学习笔记,学习爬虫,看本篇就够了
String self generated code within a given range
Houdini > rigid body, rigid body breaking RBD
TA notes of Zhuang understand (zero) < bedding and learning methods >
《内网安全攻防:渗透测试实战指南》读书笔记(七):跨域攻击分析及防御
Internal network security attack and defense: a practical guide to penetration testing (VII): cross domain attack analysis and defense
Reptile learning notes, learning reptile, read this article is enough
关于unity获取真实地理地图转3D化的相关链接
Enterprise wechat login free jump self built application
[NLP notes] preliminary study on CRF principle
每天工作4小时的程序员
Daily question | fear dominated by reverse linked list
Use of command line parameter passing library argparse
One of event management