当前位置:网站首页>C语言字符串的一些使用注意事项
C语言字符串的一些使用注意事项
2022-06-22 05:03:00 【姜学迁】
字符串输入问题
字符串输入函数:
- scanf(“%s”, str) 读入一串字符,以
空格、回车、制表符或EOF为分隔符号,空格、回车、制表符或EOF不会被读入。分隔符号后的字符将作为下一输入项处理。 - gets(str) 读入一行字符,以
回车或EOF为分隔符号,回车或EOF不会被读入。
不能使用赋值语句将一个字符串常量或字符数组直接赋给一个字符数组。字符数组名是一个地址常量,它不能改变值,正如数值型数组名不能被赋值一样。如下面两行都是不合法的:
str1 = "China";
str2 = str1;
用赋值语句只能将一个字符赋给一个字符型变量或字符数组元素。所以快捷的实现方法是使用strcpy函数将一个字符串复制到另一个字符数组中去。
字符串复制函数:strcpy(str1, str2) 把字符串str2复制到字符数组str1中去(包括’\0’)。 字符串str2的位置也可以是一个字符串常量。若str2有效字符位数 <= str1,则会部分或完全覆盖。若str2有效字符位数 > str1,则会报错。
字符串指定位复制函数:strncpy(str1, str2, n) 将字符串str2中前面n个字符复制到字符数组str1中去,取代str1中原有的最前面n个字符。
字符串长度问题
我们在对字符数组进行初始化时,从中括号里写不写数字来看有两种写法:
- 中括号写数字
A. 数字与实际大小相等:系统不会在后面加'\0',字符个数等于数组大小等于数字。
B. 数字与实际大小不等:系统会对char str[6] = "abc123"; char str[6] = { "abc123"}; char str[6] = { 'a', 'b', 'c', '1', '2', '3'};未初始化元素置'\0',字符个数与数字不等,数组大小与数字相等。char str[8] = "abc123"; //"abc123\0\0" char str[8] = { "abc123"}; //{"abc123\0\0"} char str[8] = { 'a', 'b', 'c', '1', '2', '3'}; //{'a','b','c','1','2','3','\0','\0'} - 中括号不写数字:下面的前两种情况系统会
自动在末尾加'\0',此时数组大小等于字符个数加一。第三种情况则不同,数组长度等于字符个数。char str[] = "abc123"; //"abc123\0" char str[] = { "abc123" }; //{"abc123\0"} char str[] = { 'a', 'b', 'c', '1', '2', '3' }; //故采用该方法定义时,我们一般手动加上'\0',如下: //char str[] = { 'a', 'b', 'c', '1', '2', '3', '\0' };
而我们一般认为的字符串长度,指的是字符数组的有效字符个数,所以不包括 ‘\0’。
故而使用sizeof()计算的字符数组长度是不能用来当做字符串长度的,所以我们通常使用strlen()函数。strlen()函数通过'\0'来判断字符串结束位置,并返回字符串的有效字符个数,计算出字符串长度。
其他字符串函数
字符串拼接函数:strcat(str1, str2)
把字符串str2拼接到字符串str1的后面(包括'\0'),结果放在str1字符数组中,故而必须保证str1数组大小满足要求。函数调用后得到的返回值是str1字符数组的地址。
字符串比较函数:strcmp(str1, str2)
用来比较str1和str2,比较规则是将两个字符串自左至右逐个字符按ASCII码值大小比较,直到出现不同的字符或遇到'\0'为止。其实这个函数使用时我们更关心它是否相等的结果,如果全部字符相同,则认为两个字符串相等。反而并不关心谁比谁大,字符串比大小没什么意义。
- 若两个字符串相同,返回0。
- 若str1>str2,返回一个正整数。
- 若str1<str2,返回一个负整数。
字符串大小写转换函数
- strlwr():将字符串中的
大写字母转换为小写字母。 - strupr():将字符串中的
小写字母转换为大写字母。
边栏推荐
- Monorepo silk slide methodology: reference module hot update
- Golang concise architecture practice
- 【故障诊断】切图代码切不出来,slide.read_region不运行,但程序不报错
- numpy库常用知识整理
- 【科研笔记】关于使用openslide切图的下采样倍数
- mysql day03课堂笔记
- Virtual address space
- Gavin's perception of transformer live class - NLU and policies data decryption, source code analysis and practice of insurance BOT in the insurance industry of Rasa dialogue robot project (57)
- 基于深度学习的目标检测算法面试必备(RCNN~YOLOv5)
- Postman uses (reads) JSON files for batch testing
猜你喜欢

Pull down refresh, push up load (easy to use, finally)

In 2022, the postgraduate entrance examination ran aground | how to counter attack famous IT enterprises and become a programmer in three months

Solutions to MySQL 8.0 public key retrieval is not allowed errors

Detailed explanation of deep learning technology for building an image search engine that can find similar images

ORA-15063: ASM discovered an insufficient number of disks for diskgroup 恢複---惜分飛

The application of RPC technology and its framework sekiro in crawler reverse, encrypting data is a shuttle!

Flynk deployment mode summary

ORA-15063: ASM discovered an insufficient number of disks for diskgroup 恢复---惜分飞

flink部署模式总结

Use echart to draw 3D pie chart, dashboard and battery diagram
随机推荐
Flynk deployment mode summary
Getting the return value of DPI function is always 96 | getting the return value of DPI function is incorrect | why the return value of getdpiformonitor/getdevicecaps is incorrect
VirtualBox 6.1.34 发布
【科研笔记】Focal Loss
Compilation des connaissances communes de la base de données numpy
MySQL day01 class notes
Reconstructing thinking series 2-functions and variables
[sdx12] use qcmap_ CLI startup WiFi operation instructions
[details] domestic website filing process and steps
Will swift compile to native code- Does Swift compile to native code?
[fault diagnosis] using multithreading, the program does not report an error but does not run
NFC authentication makes the ultimate KYC experience
[fault diagnosis] cv2 Imwrite cannot write the picture, but the program does not report an error
Accelerate the promotion of industrial Internet, and map out a new blueprint for development
Data backup and recovery
MySQL day02 class notes
Remote dictionary server (redis) - a kV based NoSQL database management system used as a cache
【故障诊断】stitch.py脚本失效
[scientific research notes] about using the downsampling multiple of openslide cut
ORA-15063: ASM discovered an insufficient number of disks for diskgroup 恢复---惜分飞