模拟实现strncpy,strncat,strncmp
<1.>strncpy相比于strcpy增加了size_t参数可以实现最多拷贝的字节数
<2.>(size_t不可以超出拷贝存放的内存大小)来保证不会超出destanaton的内存,
<3.>但是需要注意的是,如果你需要拷贝的长度小于源字符串长度,那么strncpy不会在末尾加'\0'
#include<stdio.h>
#include<assert.h>
#include<string.h>
char* Strncpy(char* destination, const char* source, size_t num)
{
size_t i = ;
while (i<num)
{
destination[i] = source[i];
++i;
}
return destination;
}
int main()
{
char str1[] = "To be or not to be";
char str2[];
char str3[];
//拷贝最大的字节
Strncpy(str2, str1, sizeof(str2));
//拷贝小于str1的字节
Strncpy(str3, str2, );
str3[] = '\0'; //需要自己加如'\0'
puts(str1);
puts(str2);
puts(str3);
return ;
}
<1.>现在destination函数中找到'\0',然后将source函数中的num个字符拼接到destination中
<2.>注意source函数不能修改,需要加const
<3.>返回结果是指针类型
#include<stdio.h>
#include<assert.h>
char* Strncat(char* destination, const char* source, size_t num)
{
//检查合法性
assert(destination != NULL);
assert(source != NULL);
//找到destination中的'\0'
int i = ;
while (destination[i]!='\0')
{
i++;
}
//拼接字符串
for (int j = ; num > ; i++, j++, num--)
{
destination[i] = source[j];
}
destination[i] = '\0';
return destination;
}
int main()
{
char str1[] = "abcd";
char str2[] = "efghijklmn";
//拼接前4个字符
Strncat(str1,str2,);
puts(str1);
printf("\n");
//拼接全部字符,(因为上一个Strncat已经改变了str1的值)
Strncat(str1, str2, sizeof(str2));
puts(str1);
return ;
}
<1.>strncmp返回值与strcmp一样
<2.>如果str1与str2前num个字符一样则返回0
<3.>str1小于str2则返回一个小于0的数
<4.>str1大于str2则返回一个大于0的数
#include <stdio.h>
#include<assert.h>
#include<string.h>
int Strncmp(const char* str1, const char* str2, size_t num)
{
assert(str1 != NULL);
assert(str2 != NULL);
while (num>)
{
if (*str1 > *str2)
{
return ;
}
else if (*str1 < *str2)
{
return -;
}
else if (*str1 == *str2)
{
str1++;
str2++;
num--;
}
else
{
str1++;
str2++;
num--;
}
}
return ;
}
int main()
{
char str1[] = "abcdef";
char str2[] = "abty";
//模拟实现Strncmp
printf("%d\n", Strncmp(str1, str2, ));//前两个字符相等
printf("%d\n", Strncmp(str1, str2, ));//前三个字符不相等
printf("%d\n", Strncmp("abcde", "abc", ));//str1大于str2
printf("%d\n", Strncmp("abc", "abcde", ));//str1小于str2
printf("\n");
//库函数strncmp
printf("%d\n", strncmp(str1, str2, ));
printf("%d\n", strncmp(str1, str2, ));
printf("%d\n", strncmp("abcde", "abc", ));
printf("%d\n", strncmp("abc", "abcde", ));
return ;
}
模拟实现strncpy,strncat,strncmp的更多相关文章
- C语言之库函数的模拟与使用
C语言之库函数的模拟与使用 在我们学习C语言的过程中,难免会遇到这样的一种情况: 我们通常实现一个功能的时候,费尽心血的写出来,却有着满满的错,这时却有人来告诉你说:这个功能可以用相应的库函数来实现. ...
- C/C++中经常使用的字符串处理函数和内存字符串函数
一. 字符处理函数 1. 字符处理函数:<ctype.h> int isdigit(int ch) ;//是否为数字,即ch是否是0-9中的字符 int ...
- C语言扫盲及深化学习
c语言特点: (1)效率高 (2)控制性强 (3)硬件亲和性好 (4)可移植性高 一.关于注释 c语言中注释不能嵌套,因此注释代码时一定要注意源代码中是否已经存在注释.要从逻辑上删除一段代码,利用预编 ...
- C语言字符,字符串,字节操作常用函数
strlen 这个函数是在 string.h 的头文件中定义的 它的函数原型是 size_t strlen( const char ); size_t 是一个无符号整型,是这样定义的 typedef ...
- C语言-字符串函数的实现(一)之strlen
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- 字符串函数的实现(三)之strcat
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- C语言-字符串函数的实现(五)之strstr
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- C语言-字符串函数的实现(二)之strcpy
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- 华为C语言编程规范
DKBA华为技术有限公司内部技术规范DKBA 2826-2011.5C语言编程规范2011年5月9日发布 2011年5月9日实施华为技术有限公司Huawei Technologies Co., Ltd ...
随机推荐
- net core 获取网站目录
AppContext.BaseDirectory 获取项目的根目录
- JavaWeb基础-Session和Cookie
JSP状态管理 http的无状态性,服务器不会记得发送请求的浏览器是哪一个 保存用户状态的两大机制:session和cookie Cookie:是web服务器保存在客户端的一系列文本信息 作用:对特定 ...
- matlab运行中出现“Caught "std::exception" Exception message is: Message Catalog MATLAB:builtins was not loaded from the file."
在我运行过程中,经常爆出这一不确定是什么的问题,经排查后发现,原来是fopen 文件后,没有及时fclose导致的.
- 使用kafka和zookeeper 构建分布式编译环境
1:在每台机器上安装jdk, 脚本代码如下: 每一个机器上下载jdk,zookeeper,kafka 链接:https://www.oracle.com/technetwork/java/javase ...
- L245
The State Council will lay down new rules that aim to make management compatible with internationall ...
- apache .htacess
htaccess 详解 .htaccess是什么 .htaccess文件(或者"分布式配置文件")提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多 ...
- phpexcel 的使用
首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...
- ubantu 安装mysql 5.7 解决安装不提示设置密码问题
1.安装Mysql sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install l ...
- python 基础-爬虫-数据处理,全部方法
生成时间戳 1. time.time() 输出 1515137389.69163 ===================== 生成格式化的时间字符串 1. time.ctime() 输出 Fri Ja ...
- Englis(二)
turn a year older 年长一岁 the birthday person 过生日的人 in honor of 为庆祝,为纪念 to observe/celebrate birthday ...