当前位置:网站首页>[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 码值,所以返回一个 负值.
边栏推荐
猜你喜欢
随机推荐
一点点读懂cpufreq(一)
Ts——项目实战应用enum枚举
湖仓一体电商项目(五):内网穿透工具-网云穿
使用代理对象执行实现类目标方法异常
Shell expect 实战案例
Using ngrok to optimize web pages on raspberry pi (2)
postman接口测试
【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
一招包治pycharm DEBUG报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode
【游戏建模模型制作全流程】在ZBrush中雕刻恶魔城男性角色模型
Qt中的常用控件
期货开户哪个平台好,要正规安全的
现在学习次世代3D游戏建模还能找到高薪好工作吗
后排乘客不系安全带?事故瞬间被甩出
Controller层代码这么写,简洁又优雅!
PAN3020 Sub-1G无线收发芯片
生成回文数
DREAMWEAVER8 part of the problem solution
轮播图动态渲染
Jbpm3.2 开发HelloWorld (简单请假流程)客户端












