当前位置:网站首页>[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
2022-08-04 22:51:00 【Albert Edison】


前言
通过上一篇文章的学习,我们发现:
strcpy 是将一个字符串全部拷贝到另一个字符串;
strcat 是将一个字符串全部追加到另一个字符串后面;
strcmp 也是比较两个字符串的全部内容;
Such operation functions are called 长度不受限制 的字符串操作函数.
那么我们如果操作字符串时并不想操作整个字符串,而只想操作字符串的一部分怎么办呢?
库函数中的 strncpy、strncat、strncmp 便解决了这个问题.
1. strncpy - 字符串拷贝(长度受限制)
char* strncpy(char* destination, const char* source, size_t num);
strncpy 的参数与 strcpy 相比较多出了一个参数,而这个参数就是需要被操作的字符个数.
strncpy :拷贝 num 个字符从 源字符串 source 到 目标空间 destination.
1、如果源字符串的长度小于 num,则拷贝完源字符串之后,在目标的后边追加 0,直到 num 个.
2、当操作数小于等于源字符串中的字符个数时,操作数的大小决定被拷贝的字符个数.
3、When the operand is greater than the number of characters in the source string,strncpy 函数将源字符串中的字符拷贝到目标空间后不够的将用 \0 填充.
代码示例
#include <stdio.h>
#include <string.h>
int main()
{
char str0[] = "xxxxxxxxxxx";
char str1[] = "xxxxxxxxxxx";
char str2[] = "hello";
strncpy(str0, str2, 5);
strncpy(str1, str2, 8);
printf("%s\n", str0);
printf("%s\n", str1);
return 0;
}
运行结果
当操作数为 5 时,拷贝结束后 str0 数组中存放的是helloxxxxxx\0;
而当操作数为 8 时,拷贝结束后 str1 数组中存放的是hello\0\0\0xxx\0.
2. strncat - 字符串追加(长度受限制)
char* strncat(char* destination, const char* source, size_t num);
strncat 的参数与 strcat 相比较也多出了一个参数,而这个参数也就是需要被操作的字符个数.
1、当操作数小于源字符串中的字符个数时,操作数的大小决定被追加的字符个数,并在追加完后再追加一个 \0.
2、当操作数大于等于源字符串中的字符个数时,将源字符串内容全部追加到目标空间便结束追加.
代码示例
#include <stdio.h>
#include <string.h>
int main()
{
char str0[10] = "abc\0xxxxx";
char str1[10] = "abc\0xxxxx";
char str2[] = "def";
strncat(str0, str2, 3);
strncat(str1, str2, 5);
printf("%s\n", str0);
printf("%s\n", str1);
return 0;
}
运行结果
当操作数为 3 时,拷贝结束后 str0 数组中存放的是abcdef\0xx\0;
而当操作数为 5 时,拷贝结束后 str1 Also stored in the arrayabcdef\0xx\0;
也就是说,如果 操作数 大于 str2 数组中的内容,那么就只把 str2 Just copy and append the content that should be in the past,don't consider anything else;
3. strncmp - 字符串比较(长度受限制)
int strncmp(const char* str1, const char* str2, size_t num);
strncmp 的参数与 strcmp 相比较也多出了一个参数,而这个参数也就是需要比较的字符个数.
比较到出现另个字符不一样或者一个字符串结束或者 num 个字符全部比较完.
代码示例
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcde";
char str2[] = "abcdf";
int ret1 = strncmp(str1, str2, 4);
int ret2 = strncmp(str1, str2, 5);
printf("%d\n", ret1);
printf("%d\n", ret2);
return 0;
}
运行结果
当操作数为 4 时,我们只比较了 str1 和 str2 的前 4 个字符,而它们前4个字符都相同,所以返回的是 0;
而当操作数为 5 的时候,我们比较了 str1 和 str2 的前 5 个字符,因为字符e的 ASCII 码值 小于 字符f的 ASCII 码值,所以返回一个 负值.
边栏推荐
猜你喜欢

enumerate()函数

未来我们还需要浏览器吗?(feat. 枫言枫语)

BUG | The interface returns abnormal data

【2020】【Paper Notes】Metasurfaces: Multifunctional and Programmable——

Using ngrok to optimize web pages on raspberry pi (2)

【转载】kill掉垃圾进程(在资源管理器占用的情况下)

使用cpolar优化树莓派上的网页(2)

一点点读懂cpufreq(一)

Deep Learning RNN Architecture Analysis

深度学习 RNN架构解析
随机推荐
temp7777
使用代理对象执行实现类目标方法异常
【3D建模制作技巧分享】zbrush贴图映射小技巧
MySQL的JSON 数据类型2
一点点读懂regulator(二)
the warmest home
Pytest learning - fixtures
PHP(3)
go语言的日志实现(打印日志、日志写入文件、日志切割)
期货开户哪个平台好,要正规安全的
从“草原牛”到“数字牛”:蒙牛的数字化转型之道
【项目实战】仿照Room实现简单管理系统
备战9月,美团50道软件测试经典面试题及答案汇总
[Paper Notes KDD2021] MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
【3D建模制作技巧分享】ZBrush如何重新拓扑
当panic或者die被执行时,或者发生未定义指令时,如何被回调到
PAN3020 Sub-1G无线收发芯片
被领导拒绝涨薪申请,跳槽后怒涨9.5K,这是我的心路历程
Linux系统重启和停止Mysql服务教程
逆序对的数量


