Do not use library functions, written by yourself (strlen, strcpy, strcmp, strcat, memcmp, memcpy, Memmove)

Source: Internet
Author: User

function to find the length of a string
int My_strlen (const char* PSTR)
{
ASSERT (PStr! = NULL);
int length = 0;
while (*pstr++! = ')
{
length++;
}
return length;
}
String copy function
char* my_strcpy (char* strdest, const char* STRSRC)
{
ASSERT (strdest = null && STRSRC! = null);
char* pDest = strdest; Protection parameters
Const char* PSRC = STRSRC;
while (*psrc! = ')
{
*pdest++ = *psrc++;
}
*pdest = ' + ';
return strdest;
}
string comparison function
int my_strcmp (const char* strdest, const char* STRSRC)
{
ASSERT (strdest = null && STRSRC! = null);
if (*strdest = = ' &&*strsrc '! = ') '
{
return 0;
}
Const char* pDest = strdest;
Const char* PSRC = STRSRC;
int result = 0; Used to record comparison results
while (*pdest! = ' | | | *psrc! = ') '
{
if (result = *pdest-*psrc)! = 0)
{
Break
}
pdest++;
psrc++;
}
if (Result > 0)
{
result = 1;
}
else if (result<0)
{
result =-1;
}
return result;
}
String Join function
char* My_strcat (char* strdest, char* strsrc)
{
ASSERT (strdest = null && STRSRC! = null);
char* pDest = strdest;
char* PSRC = strsrc;
while (*pdest! = ')
{
pdest++;
}
while (*psrc! = ')
{
*pdest++ = *psrc++;
}
*pdest = ' + ';
return strdest;
}

/Memory Comparison function
int my_memcmp (void* src1, void* src2, int len)
{
ASSERT (SRC1 = null && SRC2! = null);
Const char* PSRC1 = (char*) src1;
Const char* PSRC2 = (char*) src2;
while (len--> 0)
{
if (*psrc1++! = *psrc2++)
{
Return *psrc1 < *PSRC2? -1:1;
}
}
return 0;
}

Memory copy function
void* my_memcpy (void* dest, const void* SRC, size_t count)
{
ASSERT (dest! = NULL && src! = null);
char* pDest = (char*) dest;
Const char* PSRC = (const char*) src;
while (count--> 0)
{
*pdest++ = *psrc++;
}
return dest;
}

Memory Move function
void* My_memmove (void* dest, void* src, size_t count)
{
ASSERT (dest! = NULL && src! = null);
char* pDest = (char*) dest;
Const char* PSRC = (char*) src;
if (pSrc >= pDest | | pDest >= (PSRC + count))
{


while (count--> 0)
{
*pdest++ = *PSRC;
}
}
Else
{
while (count--> 0)

{

* (PDest + count) = * (PSRC + count);
}
}
*pdest = ' + ';
return dest;
}

Do not use library functions, written by yourself (strlen, strcpy, strcmp, strcat, memcmp, memcpy, Memmove)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.