当前位置:网站首页>Introduction and Simulation of string function
Introduction and Simulation of string function
2022-07-17 23:16:00 【A Liang joy】
Catalog
String function with limited length
String function with limited length
This article will introduce you to several string functions with limited length , Namely strncpy、strncat and strncmp. Why are these three functions string functions with limited length ? Because when calling these functions , We need to pass one more unsigned integer data . Compared with string functions with unlimited length , String functions with limited length are also more secure . First of all, let's learn strncpy.
1.strncpy
char * strncpy ( char * destination, const char * source, size_t num );
about strncpy function , We need to pay attention to a few points :
- Copy num Characters from the source string to the target space .
- If the length of the source string is less than num, After copying the source string , Add... After the target 0, until num individual .
- The target space has to be variable .
- Simulation Implementation strncpy function
Code example :

Simulation Implementation strncpy function :
#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--;
}
// Be careful source First give dest assignment , To determine dest Is it \0
// So when source by \0 when , I will dest assignment , Push out the cycle if the condition is not met
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 );
about strncat function , We need to pay attention to a few points :
- Append the characters of the source string to the destination space , as well as '\0' .
- If the length of the source string is less than num, Copy only '\0' Previous content .
- The target space has to be variable .
- Simulation Implementation strncat function .
Code example :

Simulation Implementation strncat function
#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 );
about strncmp function , We need to pay attention : This function starts comparing the first character of each string . If they are equal , Then continue to compare backward , Until the characters are different or until '\0' Or the number of comparisons reaches num.strncmp The return value of is greater than 0、 be equal to 0 Or less than 0.
Code example

Simulation Implementation strncmp function
#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;
}The above is the content of this blog , You can try to simulate and implement these functions , Let yourself understand more deeply . If you think you've got something , You can click a like to support it !!
边栏推荐
- 浅解volatile
- ML:机器学习可解释性之特征置换重要性之机器学习模型中哪些特征很重要?
- 15.1.2、MySQL—mysql时间与时间戳转换的函数,unix_timestamp、from_unixtime
- Error: grouping factors must have > 1 sampled level
- Proxmox VE 7.2 备份恢复虚拟机
- Apache Flink 在翼支付的实践应用
- How to solve common errors of JMeter
- 股票账户上买基金安全吗。可以做短线吗
- 【安全狗】微软7月多个漏洞更新解决
- Proxmox ve 7.2 LxC deployment openwrt
猜你喜欢

Stm32f407 external SRAM

IPv6 navigation, strong sail pointing application

Proxmox VE 7.2 使用qemu-img转换磁盘格式

Software testing interface testing practice wechat official account platform postman+newman+jenkins realizes automatic report generation and continuous integration

Comment les entreprises Internet réalisent - elles la pagination, prenez MySQL Strength limit?

如何把一个表格中的数据导入到对应数据库网站中

Proxmox ve 7.2 install grafana+prometheus monitoring PVE exporter

Basic part of C language: guessing numbers games

Animation optimization

New development of Flink runtime for streaming batch integration
随机推荐
Slow SQL analysis and optimization
Government organizations improve the efficiency, transparency and control of information management through content management
source insight4 配色仿vscode
Practical application of Apache Flink in wing payment
Proxmox VE 7.2 网卡直通
Proxmox ve 7.2 importing virtual machines from CT templates
[recognize cloud Nativity] Chapter 4 cloud network section 4.9.4.1 - overview of smart NIC solutions
Serein 【懒人神器】一款图形化、批量采集url、批量对采集的url进行各种nday检测的工具 摸鱼项目问题解决
Apache Flink 在移动云实时计算的实践
Proxmox ve 7.2 converting disk formats using QEMU img
pytorch 自定义损失函数、优化器(Optimizer)和学习率策略
Stock market forecast, sales forecast, virus spread A time series modeling routine, all done!
C language emptying input residual content
打开有惊喜
Kotlin 点击空白位置隐藏软键盘
指针与数组笔试题详解
What is the user and permission system of MySQL?
TFTLCD thin film transistor liquid crystal display -- Taking Explorer as an example
Proxmox ve 7.2 LxC deployment openwrt
浅解volatile