当前位置:网站首页>C language implementation of string and memory library functions
C language implementation of string and memory library functions
2022-06-12 13:24:00 【shlyyy】
C Language to handle string functions and memory functions
Preface
Their own string processing functions and memory processing functions . Contains only stdio.h file , Do not include third-party libraries .
About the function of each function , Please refer to the link below
Chinese Links
English links
One 、str
1.1 strlen
size_t MyStrlen(const char* str)
{
size_t i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}
1.2 strcmp
int MyStrcmp(const char* str1, const char* str2)
{
char* String1 = (char*)str1;
char* String2 = (char*)str2;
for (; (*String1 == *String2); String2++)
{
// if(*String1)
// String1++;
if (!*String1++)
{
return (0);
}
}
return ((unsigned char)*String1 - (unsigned char)*String2);
}
1.3 strstr
char* MyStrstr(const char* pszDst, const char* pszTag)
{
char* pDest = (char*)pszDst;
char* pRet = NULL;
size_t i = 0;
while (*pDest != '\0')
{
if (*pDest == *pszTag)
{
i = 0;
while (pDest[i] == pszTag[i])
{
if (pszTag[i + 1] == '\0')
{
pRet = pDest;
return pRet;
}
i++;
}
}
pDest++;
}
return pRet;
}
1.4 strcpy
char* MyStrcpy(char* strDestination, const char* strSource)
{
size_t i = 0;
while (strSource[i] != '\0')
{
strDestination[i] = strSource[i];
i++;
}
strDestination[i] = strSource[i];
return strDestination;
}
1.5 strcat
char* MyStrcat(char* strDestination, const char* strSource)
{
size_t i = 0;
size_t j = 0;
while (strDestination[i] != '\0')
{
i++;
}
while (strSource[j] != '\0')
{
strDestination[i] = strSource[j];
j++;
i++;
}
strDestination[i] = strSource[j];
return strDestination;
}
Two 、mem
2.1 memset
void* MemorySet(void* pDest, int nValue, size_t nCount)
{
while (nCount > 0)
{
((char*)pDest)[nCount - 1] = nValue;
nCount--;
}
return pDest;
}
2.2 memcpy
void* MemoryCopy(void* pDest, const void* pSrc, size_t nCount)
{
char* pNew = (char*)pDest;
char* pOld = (char*)pSrc;
if (pNew < pOld) // From front to back
{
while (nCount != 0)
{
*pNew = *pOld;
pNew++;
pOld++;
nCount--;
}
}
else // From the back to the front
{
pNew = pNew + nCount - 1;
pOld = pOld + nCount - 1;
while (nCount != 0)
{
*pNew = *pOld;
pNew--;
pOld--;
nCount--;
}
}
return pDest;
}
3、 ... and 、swap
void MySwap(int* const n1, int* const n2)
{
int nTemp = *n1;
*n1 = *n2;
*n2 = nTemp;
}
边栏推荐
- Redis message queue repeated consumption
- 移动应用出海的“新大陆”
- import torch_ Data view of geometric
- hudi 键的生成(Key Generation)
- Hudi key generation
- [cloud native | kubernetes] kubernetes networkpolicy
- Symbolic constant, const qualifier
- 1001:Hello,World
- Will the next star of PPT for workplace speech be you [perfect summary] at the moment
- Title: Yanghui triangle
猜你喜欢

移动应用出海的“新大陆”

章鱼网络进展月报 | 2022.5.1-5.31

简历 NFT 平台 TrustRecruit 加入章鱼网络成为候选应用链

Resume NFT platform trustrecruit joined Octopus network as a candidate application chain

Bitmap, bloom filter and hash sharding

多源BFS问题 模板(附题)

成功跳槽阿里,进阶学习

torch_geometric mini batch 的那些事

Automatic Generation of Visual-Textual Presentation Layout

看完这一篇就够了,web中文开发
随机推荐
1003: align output
JSP jump problem, unable to display database data, and unable to jump
Informatics Olympiad all in one 1000: introductory test questions
verilog-mode的简要介绍
Unittest framework
Implementing pytorch style deep learning framework similartorch with numpy
实战 | 巧用位姿解算实现单目相机测距
[embedded] serial communication and its case
leetcode 47. Permutations II full permutations II (medium)
Vant tab bar + pull-up loading + pull-down refresh demo van tabs + van pull refresh + van list demo
RK3399平台开发系列讲解(内核调试篇)2.50、systrace的使用
镜像扫描工具预研
Pytorch framework
NVIDIA Jetson Nano Developer Kit 入门
jsp跳转问题,不能显示数据库数据,并且不能跳转
Introduction to application design scheme of intelligent garbage can voice chip, wt588f02b-8s
How to solve the problem of data table query error when SQLite writes the registration function?
hudi 键的生成(Key Generation)
Software construction 03 regular expression
Redis message queue repeated consumption