当前位置:网站首页>String function and memory function (2)
String function and memory function (2)
2022-07-25 18:34:00 【Caicaixiaomeng】
Today, let's learn about the implementation of string functions and memory functions .
Catalog
( One ) String function
(1)strlen
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
size_t my_strlen(const char* pc)
{
assert(pc);
int count = 0;
while ((*pc) != '\0')
{
count++;
pc++;
}
return count;
}
int main()
{
char arr[] = "l love you 3000!";
int ret = my_strlen(arr);
printf("%d\n", ret);
return 0;
}(2)strcpy
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* str1, const char* str2)
{
char* dest = str1;
assert(str1 != NULL);
assert(str2 != NULL);
while (*dest++ = *str2++)
{
;
}
return str1;
}
int main()
{
char arr1[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
char arr2[] = "I love you 3000!";
printf("%s\n", my_strcpy(arr1, arr2));
return 0;
}(3)strcat
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* source)
{
assert(dest && source);
char* ret = dest;
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *source++)
{
;
}
return ret;
}
int main()
{
char arr[20] = "Hello ";
my_strcat(arr, "world");
puts(arr);
return 0;
}(4)strcmp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* ch1, const char* ch2)
{
assert(ch1 && ch2);
while (*ch1 == *ch2)
{
if (*ch1 == 0)
return 0;
ch1++;
ch2++;
}
return (*ch1 - *ch2);
}
int main()
{
char arr1[] = "abcd";
char arr2[] = "abcde";
int ret = my_strcmp(arr1, arr2);
if (ret > 0)
printf(">\n");
else if (ret == 0)
printf("=\n");
else
printf("<\n");
return 0;
}(5)strncpy
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strncpy(char* destination, const char* source, size_t num)
{
assert(destination && source);
char* ret = destination;
while (num--)
{
*destination = *source;
destination++;
source++;
}
return ret;
}
int main()
{
char arr1[] = "abcdef";
char arr2[10] = "xxxxxxxxx";
my_strncpy(arr2, arr1, 5);
puts(arr2);
return 0;
}
(6)strncat
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strncat(char* destination, const char* source, size_t num)
{
assert(destination && source);
char* ret = destination;
while (*destination != 0)
{
destination++;
}
while (num--)
{
*destination = *source;
destination++;
source++;
}
*++destination = '\0';
return ret;
}
int main()
{
char arr1[10] = "abc";
char arr2[] = "defgh";
my_strncat(arr1, arr2, 4);
puts(arr1);
return 0;
}( Two ) Memory function
(1)memcpy
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
void* my_memcpy(void* dest, const void* src, size_t count)
{
assert(dest && src);
void* ret = dest;
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
my_memcpy(arr2, arr1, 40);
printf("arr2:");
for (int i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}(2)memmove
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t count)
{
assert(dest && src);
void* ret = dest;
if (dest < src)
{
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
while (count--)
{
*((char*)dest - count) = *((char*)src - count);
}
}
return ret;
}
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
memcpy(arr, arr + 2, 20);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}边栏推荐
- Analysis of regression problem, modeling and prediction
- Interview shock: why does TCP need three handshakes?
- C language libcurl cross compilation
- [web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
- Register carefully! The number of applicants for these double non-governmental institutions exceeded 10000!
- PHP memory management mechanism and garbage collection mechanism
- Use of C language cjson Library
- Number two 2010 real test site
- 结合GHS MULTI使用瑞萨E1仿真器实现对瑞萨RH850单片机的仿真调试
- 11.1-cm24 nearest common ancestor
猜你喜欢

什么是3DE体验平台

Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution

Powell's function of Ceres

Number two 2010 real test site

Imx6 rtl8189ftv migration

Thales launches solutions to help SAP customers control cloud data

Cve-2022-33891 Apache spark shell command injection vulnerability recurrence

pd.melt() vs reshape2::melt()

MySQL index optimization introduction

如何创建一个有效的帮助文档?
随机推荐
Pan domain name configuration method
srec_ Use of common cat parameters
Use of C language cjson Library
Detailed introduction and application of GaN (comprehensive and complete)
这是一张机器&深度学习代码速查表
win11下vscode 自动升级失败 There was an error while marking a file for deletion
【翻译】Logstash、Fluentd、Fluent Bit,还是Vector?如何选择合适的开源日志收集器...
Regex 正则表达式
11.1-CM24 最近公共祖先
Qtimgui 编译
JZ32 从上往下打印二叉树
Software testing -- common testing tools
Chapter 5 Basic Scripting: Shell Variables
关爱一线防疫工作者,浩城嘉业携手高米店街道办事处共筑公益长城
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
Jz71 jump step expansion problem
11.2-HJ86 求最大连续bit数
7. 依赖注入
Cve-2022-33891 Apache spark shell command injection vulnerability recurrence
Design practice of Netease strictly selecting inventory center