当前位置:网站首页>库函数的模拟实现-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;
}边栏推荐
猜你喜欢

Amazon Cloud Technology Build On 2022 - AIot Season 2 IoT Special Experiment Experience

第三章 标准单元库(上)

Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?

The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation

tmux concept and usage

No matching function for call to 'RCTBridgeModuleNameForClass'

MNIST手写数字识别 —— 从零构建感知机实现二分类

亚马逊云科技Build On-Amazon Neptune基于知识图谱的推荐模型构建心得

双向LSTM

Detailed steps to install MySQL
随机推荐
基于BiGRU和GAN的数据生成方法
第三章 标准单元库(下)
安装pyspider后运行pyspider all后遇到的问题
卷积神经网络入门详解
Rules.make - suitable for viewing in edit mode
MNIST手写数字识别 —— Lenet-5首个商用级别卷积神经网络
管道重定向
安装MySQL的详细步骤
LeetCode_Dec_3rd_Week
Tensorflow/Pytorch安装(Anaconda环境下,无版本冲突,亲测有效)
target has libraries with conflicting names: libcrypto.a and libssl.a.
在AWS-EC2中安装Minikube集群
集合---ArrayList的底层
[开发杂项][调试]debug into kernel
file permission management ugo
中国联通、欧莱雅和钉钉都在争相打造的秘密武器?虚拟IP未来还有怎样的可能
Completely remove MySQL tutorial
Pytest common plug-in
makefile基础学习
Cut the hit pro subtitles export of essays