Use and coding implementation of string functions strlen(), strcat(), strcpy(), strcmp() and memory operation functions memset(), memcpy(), memcmp(), memmove() in C language

The use and coding of string functions strlen(), strcat(), strcpy(), strcmp() and memory operation functions memset(), memcpy(), memcmp(), memmove() in c language to realize the function starting with str
, The operation object is a string, and the function starting with mem is the memory
1. The strlen () function
finds the effective length of the string, excluding '\0'
. The realization of the strlen () function:
#include
#include
#include
size_t mystrlen( char *str)
{ assert (
   ste != NULL);
   const char*p = str;
   size_t count = 0;
   while(*p++ != '\0')
   {
      count++;
   } return
   count; Calculate the number of characters size_t mystrlen(char *str) {
   




  assert(str != NULL);
  if(*str == '\0')
    return 0;
  else
  return mystrlen(str+1)+1;
}
2. strcat() string connection function
strcat(s1, s2) put The string s2 is connected to the back of s1, pay attention to ensure that s1 has enough space
to implement the function:
char *mystrcat(char *strD, const char *strS)
{
   assert(strD!=NULL&&strS!=NULL);
   char *pD = strD;
   const char *pS = strS;
   while(*pD != '\0')
  {
     pD++;
   }
   while(*pS != '\0')
   {*pD++ = *pS++;}
   *pD = '\0';
   return strD;
}
3. strcpy() string copy function
strcpy (s1, s2) copy string s2 to s1
char *mystrcpy(char *strD,const char *strS)
{
   assert(strD != NULL&&strS !=NULL);
   char *pD = strD;
   const char *pS = strS ;
   while(*pS !='\0')
   {*pD++ = *pS++;}
   *pD = '\0';
   return strD;
}
4. strcmp() string comparison function
int mystrcmp(const char *str1, const char *str2)
{
  assert(str1 != NULL&& str2 != NULL);
  const char *pstr1 = str1;
  const char *pstr2 = str2;
  int result = 0;
  while(*pstr != '\0'||*pstr2 != '\0')
  { 
     result = *pstr1 - *pstr2;
     if(result != 0)
     {break;}
      pstr1++;
      pstr2++;    
  }
  if(result > 0)
  result = 1;
  else if(result < 0)
  result = -1;
  return result;
}
5. memset() memory setting or initialize function
int array[10];
memset(array, 0, sizeof(int)*10);
array array initialized with 0
void *mymemset(void *dest, int c, size_t count)
{
   assert(dest != NULL);
   char *p = (char *)dest;
   while(count-- >0)
  {*p++ = c;}
   return dest;
}
6. memcpy() memory copy function
The memory copy function can not only copy strings, but like the strcpy() function, it can copy any type of object
Function implementation: avoid overwriting
void *mymemcpy(void *dest,const void *src,size_t count)
{
   assert(dest ! = NULL||src != NULL);
   char *pD = (char *)dest;
   const char *pS = (const char*)src;
   if(pD>=pD||pD>=pS+counst)
  {
    while( count--)
    {*pD++ = *pS++;}
  }
  else
  {
    while(count-- > 0)
    {*(pD+count)=*(pS+count)}
  }
  return dest;
}
7. memcmp() memory comparison function
int mymemcmp(const void *buf1, const void *buf2, size_t count)
{
  assert(buf1 != NULL || buf2 != NULL)
  const char *pbuf1 = (const char *)buf1;
  const char *pbuf2 = (const char *)buf2;
  int res = 0;
  while(count-- > 0)
  {
    if((res = *pbuf++ - *pbuf2++)!=0)
    {break;}
    if(res> 0)
    {res = 1;}
    if(res < 0)
    {res = -1;}
    reurn res;
  }
}
8、memmove()内存移动函数
函数实现
void *mymemmove(void *dest,const void *src,size_t count)
{
  assert(dest != NULL || src != NULL);
  char *pD = (char *)dest;
  const char *pS = (const char*)src;
  if(pS >= pD||pD>=pS+count)
  {
     while(count-- >0)
     {*pD++ = *pS++;}
  }
  else
  {
    while(count-- >0)
    {*(pD+count)=*(pS+count);}
  }
  return dest;
}

















Guess you like

Origin blog.csdn.net/liujing_sy/article/details/76273855
Recommended