当前位置:网站首页>字符串函数的介绍及模拟实现
字符串函数的介绍及模拟实现
2022-07-15 08:50:00 【阿亮joy.】
目录
长度受限制的字符串函数
这篇文章将会向大家介绍几个长度受限制的字符串函数,分别是strncpy、strncat和strncmp。为什么这三个函数是长度受限制的字符串函数呢?因为在调用这些函数时,我们需要多传一个无符号整型数据。相较于长度不受限制的字符串函数,长度受限制的字符串函数也更加安全。首先我们先来学习strncpy。
1.strncpy
char * strncpy ( char * destination, const char * source, size_t num );
对于strncpy函数,我们需要注意一下几点:
- 拷贝num个字符从源字符串到目标空间。
- 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
- 目标空间必须可变。
- 模拟实现strncpy函数
代码示例:

模拟实现strncpy函数 :
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strncpy(char* dest, char* source, int n)
{
assert(dest && source);
char* start = dest;
while (n && ((*dest++ = *source++) != '\0'))
{
n--;
}
//注意source先给dest赋值,再判断dest是否为\0
//所以当source为\0时,也会给dest赋值,不满足条件推出循环
if (n)
{
while (--n)
{
*dest++ = '\0';
}
}
return start;
}
int main()
{
char arr1[30] = "remember who you are!";
char arr2[] = "believe yourself!";
my_strncpy(arr1, arr2, 30);
printf("%s\n", arr1);
return 0;
}2.strncat
char * strncat ( char * destination, const char * source, size_t num );
对于strncat函数,我们需要注意一下几点:
- 将源字符串的字符追加到目标空间,以及'\0' 。
- 如果源字符串的长度小于num,则仅复制'\0'之前的内容。
- 目标空间必须可变。
- 模拟实现strncat函数。
代码示例 :

模拟实现strncat函数
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strncat(char* dest, char* source,int n)
{
assert(dest && source);
char* start = dest;
while (*dest != '\0')
{
dest++;
}
while (n)
{
*dest++ = *source++;
n--;
if (*source == '\0')
{
return start;
}
}
*dest = '\0';
return start;
}
int main()
{
char arr[20] = "hello ";
char arr2[] = "ww";
my_strncat(arr, arr2, 2);
printf("%s\n", arr);
return 0;
}3.strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
对于strncmp函数,我们需要注意: 此函数开始比较每个字符串的第一个字符。如果它们相等,则继续向后比较,直到字符不同或直到'\0'亦或比较次数达到num。strncmp的返回值为大于0、等于0或小于0。
代码示例

模拟实现strncmp函数
#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_strncmp(const char* str1, const char* str2, int n)
{
assert(str1 && str2);
int count = 0;
while (*str1 == *str2)
{
count++;
if (*str1 == '\0')
return 0;
if (count == n)
return 0;
str1++;
str2++;
}
if (*str1 > *str2)
return 1;
else
return -1;
}
int main()
{
char arr1[] = "hello ";
char arr2[] = "hello sz";
int ret;
ret = my_strncmp(arr1, arr2, 6);
printf("ret = %d\n", ret);
ret = my_strncmp(arr1, arr2, 7);
printf("ret = %d\n", ret);
return 0;
}以上就是本篇博客的内容,大家可以尝试去模拟实现这些函数,让自己理解得更加深刻。如果大家觉得有收获的话,可以点个赞支持一下哦!!
边栏推荐
猜你喜欢

Postman interface test tool

Cyber Nuwa, how to make digital people?

App & Web testing tools
![[recognize cloud Nativity] Chapter 4 cloud network section 4.9.4.1 - overview of smart NIC solutions](/img/58/06961183acb766e65722ecbe7cc20f.png)
[recognize cloud Nativity] Chapter 4 cloud network section 4.9.4.1 - overview of smart NIC solutions

IPv6大航海,风帆指向强应用

YOLO系列论文精度 & YOLOv2 and YOLO9000

Contact Chen Xianbao. How much can he deal with when the 60 year old man returns?

745. Prefix and suffix search: General trie application questions

AttributeError: ‘DenseLayer‘ object has no attribute ‘memory_ efficient‘

Implement stack with queue
随机推荐
Session tracking technology cookies and sessions
745. Prefix and suffix search: General trie application questions
Animation optimization
c语言基础篇:二分查找
Proxmox ve 7.2 backup recovery virtual machine
YOLO系列论文精度 & YOLOv2 and YOLO9000
Basic part of C language: guessing numbers games
[solution] the solution to the unresponsive keys under the old unity inputsystem
【面试:并发篇13:多线程: 变量的线程安全分析】
zabbix故障集--当你忘记登录zabbix系统的账号密码时
C语言之数组参数,指针参数,函数指针,函数指针数组
Communication mode - FSMC
Market Research and investment strategy report of China's high purity magnesium oxide industry (2022 Edition)
C语言中的关键字struct、union、enum、typedef
Proxmox ve 7.2 network card pass through
Proxmox VE 7.2 Install Grafana+Prometheus监控 pve-exporter
WeMos和树莓派的通信
Proxmox VE 7.2 Install SMB 服务
Pickpocket reentrantlock and AQS implementation principle
软件测试 接口测试 实战 微信公众号平台 Postman+newman+jenkins 实现自动生成报告并持续集成