当前位置:网站首页>库函数的模拟实现-C语言
库函数的模拟实现-C语言
2022-08-04 05:31:00 【crazy__xieyi】
size_t my_strlen(const char* str)
{
assert(str);
size_t count = 0;
while (*str != '\0')
{
str++;
count++;
}
return count;
}
int main()
{
char arr[] = "abcdefg";
size_t len = my_strlen(arr);
printf("%u", len);
return 0;
}
//char* my_strcpy(const char* str1, char* str2)
//{
// char* ret = str2;
// assert(str1 && str2);
// while (*str1 != '\0')
// {
// *str2++ = *str1++;
//
// }
// str2 = str1;
// return ret;
//
//}
//int main()
//{
// char arr1[] = "abcdefgh";
// char arr2[10] = "";
// my_strcpy(arr1, arr2);
// printf("%s",arr2);
// return 0;
//}
char* my_strcat(char* str1, const char* str2)
{
assert(str1 && str2);
char* ret = str1;
while (*str1 != '\0')
{
str1++;
}
while (*str1++ = *str2++);
return ret;
}
int main()
{
char arr1[10] = "hello";
char arr2[] = "word";
my_strcat(arr1, arr2);
printf("%s", arr1);
return 0;
}
int my_strcmp(char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
str1++;
str2++;
}
return (*str1 - *str2);
}
int main()
{
char arr1[] = "zhangsan";
char arr2[] = "zhangsanfeng";
int ret = my_strcmp(arr1, arr2);
if (ret < 0)
{
printf("<\n");
}
else if (ret == 0)
{
printf("==/n");
}
else
{
printf(">\n");
}
return 0;
}
char* my_strncpy(char* destination, const char* source, size_t n)
{
char* cp = destination;
int i = 0;
while (*source && i < n)
{
*cp++ = *source++;
i++;
}
for (int j = i; j < n; j++)
{
*cp++ = 0;
}
return destination;
}
int main()
{
char a[20] = "abcdefghi";
char b[] = "xyz";
my_strncpy(a, b, 6);
printf("%s\n", a);
return 0;
}
如果源字符串的长度小于num,则只复制到结束的空字符的内容。
char* my_strncat(char* dest, const char* scr, int n)
{
char* p = dest;
assert(dest && scr);
while (*p)
{
p++;
}
while (n--)
{
*p++ = *scr++;
}
*p = '\0';
return dest;
}
int main()
{
char arr[32] = "hello ";
const char arr1[] = "word!";
char* str = my_strncat(arr, arr1, 20);
printf("%s\n", str);
return 0;
}
8 strstr
返回str1中第一次出现的str2的指针,如果str2不是str1的一部分,则返回一个空指针。
模拟实现:
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* s1 = str1;
const char* p = str1;
const char* s2 = str2;
while (*p)
{
s1 = p;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)p;
}
p++;
}
return NULL;
}
int main()
{
char arr1[] = "abbbcde";
char arr2[] = "bcd";
char* ret = my_strstr(arr1, arr2);
if (ret == NULL)
{
printf("子串不存在\n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
int main()
//{
// const char* sep = "@.";
// char email[] = "[email protected]";
// char cp[40] = { 0 };
// strcpy(cp, email);
//
// char* ret = NULL;
// for (ret = strtok(cp, sep);
// ret != NULL;
// ret=strtok(NULL, sep))
// {
// printf("%s\n", ret);
// }
// return 0;
//}
void* my_memcpy(void* dest, const void* src, size_t sum)
{
assert(dest && src);
void* ret = dest;
while (sum--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[] = { 0,1,2,3,4,5,6,7,8,9 };
int arr2[10] = { 0 };
my_memcpy(arr2, arr1, 16);
return 0;
}
void* my_memmove(void* dst, const void* src, size_t count) {
void* ret = dst;
if (dst <= src || (char*)dst >= ((char*)src + count)) {
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
}
else {
dst = (char*)dst + count - 1;
src = (char*)src + count - 1;
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst - 1;
src = (char*)src - 1;
}
}
return(ret);
}
int main()
{
int arr1[] = { 0,1,2,3,4,5,6,7,8,9 };
my_memmove(arr1 + 2, arr1, 16);
return 0;
}
边栏推荐
- MNIST手写数字识别 —— Lenet-5首个商用级别卷积神经网络
- 如何成长为高级工程师?
- tensorRT教程——tensor RT OP理解(实现自定义层,搭建网络)
- The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation
- No matching function for call to ‘RCTBridgeModuleNameForClass‘
- Amazon Cloud Technology Build On-Amazon Neptune's Knowledge Graph-Based Recommendation Model Building Experience
- 【Copy攻城狮日志】“一分钟”跑通MindSpore的LeNet模型
- 详解近端策略优化
- 题目1000:输入两个整数a和b,计算a+b的和,此题是多组测试数据
- SQL注入详解
猜你喜欢
题目1000:输入两个整数a和b,计算a+b的和,此题是多组测试数据
MNIST手写数字识别 —— 从二分类到十分类
强化学习中,Q-Learning与Sarsa的差别有多大?
Code to celebrate the Dragon Boat Festival - Zongzi, your heart
MOOSE平台官方第二个例子分析——关于创建Kernel,求解对流扩散方程
Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?
[CV-Learning] Linear Classifier (SVM Basics)
Install Minikube Cluster in AWS-EC2
MNIST手写数字识别 —— 从零构建感知机实现二分类
arm-3-中断体系结构
随机推荐
MNIST手写数字识别 —— 从二分类到十分类
AWS使用EC2降低DeepRacer的训练成本:DeepRacer-for-cloud的实践操作
深度确定性策略梯度(DDPG)
浅谈游戏音效测试点
安装Apache服务时出现的几个问题, AH00369,AH00526,AH00072....
代码庆端午--粽你心意
文件权限管理 ugo
Thunderbolt turns off automatic updates
LeetCode_Dec_2nd_Week
Copy Siege Lion's Annual "Battle" | Review 2020
Rules.make-适合在编辑模式下看
度量学习(Metric learning、损失函数、triplet、三元组损失、fastreid)
IDEA创建Servlet步骤
arm learning-1-development board
MNIST Handwritten Digit Recognition - Lenet-5's First Commercial Grade Convolutional Neural Network
MySQL存储过程学习笔记(基于8.0)
MNIST Handwritten Digit Recognition - Image Analysis Method for Binary Classification
MNIST手写数字识别 —— 基于Mindspore快速构建感知机实现十分类
LeetCode_Nov_3rd_Week
MNIST handwritten digit recognition, sorted by from two to ten