自己编写的string库函数

//都是自己编写的,仅供学习参考。

/*

date: 2009.6.24

author: Summon

function: functions for string.h

version: v1.0

right: all right opened

*/

char *MyStrcpy(char *pchDest, const char *pchSour)                                         
{
 if ((pchDest == NULL) || (pchSour == NULL))
 {
  return NULL;
 }

 char *ret = pchDest;

 do
 {
  *pchDest = *pchSour;
  pchDest++;
  pchSour++;
 }while (*pchSour != '/0');
 *pchDest = '/0';
 return ret;
}

 

///

char *MyStrncpy(char *pchDest, const char *pchSour, size_t count)
 {
 if ((pchDest == NULL) || (pchSour == NULL))
 {
  return NULL;
 }
    if (count == 0)
 {
  return pchDest;
 }
 char *ret = pchDest;
   
 while (count--)
 {
  *pchDest = *pchSour;
  pchDest++;
  pchSour++;
  if (*pchSour == '/0')
  {   
   break;
  }
 }
 *pchDest = '/0';
 return ret;
}

int MyStrcmp(const char *pchStr1, const char *pchStr2)
{
 if ((pchStr1 == NULL) || (pchStr2 == NULL))
 {
  printf("StrCompare input error!/n");
  exit(-1);
 }

 int ret = 0;

 while (*pchStr1 == *pchStr2)
 {
  pchStr1++;
  pchStr2++;
  if (*pchStr1 == 0)
  {
   return ret;
  }
 }
    ret = *(unsigned char *)pchStr1 - *(unsigned char *)pchStr2;
 return ret;
}
//

int MyStrlen(const char *pchDest

{
 if (pchDest == NULL)
 {
  return -1;
 }

 int ret = 0;
 while(*pchDest != 0)
 {
        ret++;
  pchDest++;
 }
   
 return ret;
}

int MyStrncmp(const char *pchStr1, const char *pchStr2, size_t count)
{
 if ((pchStr1 == NULL) || (pchStr2 == NULL))
 {
  printf("StrCompare input error!/n");
  exit(-1);
 }
 if (count == 0)
 {
  return 0;
 }

 int ret = 0;

 while (count--)
 {
  if (*pchStr1 == *pchStr2)
  {  
   if (*pchStr1 == 0)
   {
    return ret;
   }
   pchStr1++;
   pchStr2++;//必须放到上个if后面再++
   continue;
  }else
  {
   pchStr1++;//虽然if和else里都有++,但是不能放到外面用共同的代码
   pchStr2++;
   break;
  }
 }
 
    ret = *(unsigned char *)--pchStr1 - *(unsigned char *)--pchStr2;
 return ret;//必须--才能返回正确的值,否则会向后移一个字符才比较
}
/

void *MyMoveMemery(void *pDest, const void *pSrc, size_t nSize)

 if ((pSrc != NULL) && (pDest != NULL))
 {
  return NULL;
 }
 if (nSize == 0)
 {
  return NULL;
 }//input detected


    void *ret = pDest;//remain the old pointer
    unsigned char *d = (unsigned char *)pDest;//change to the most small memory unit
 unsigned char *s = (unsigned char *)pSrc;

 if (d < s + nSize)//special memory overlap
 { 
  s += nSize - sizeof(pDest);
  d += nSize - sizeof(pDest);

  while (s != ((unsigned char *)pSrc - 1))//不能写!=pSrc,否则最后一个赋值语句不能执行。
  {
   *d = *s;
   d--;
   s--;
  }
 }else
 {
  while (nSize--)
  {
   *d = *s;
            d++;
      s++;
  }
    }
    return ret;
 //return the old pointer
}

///

char *MyStrcat(char *pchDestination, const char *pchSource)
{
 if ((pchDestination == NULL) || (pchSource == NULL))
 {
  return NULL;
  exit(-1);
 }
   
 char *ret = pchDestination;

 while (*pchDestination != '/0')
 {
  pchDestination++;
 }
    while (*pchSource != '/0')
 {
  *pchDestination = *pchSource;
  pchSource++;
  pchDestination++;
 }
    *pchDestination = '/0';

 return ret;
}

char *MyStrncat(char *pchDestination, const char *pchSource, size_t count)
{
 if ((pchDestination == NULL) || (pchSource == NULL))
 {
  return NULL;
  exit(-1);
 }
 if (count == 0)
 {
  return pchDestination;
 }
   

 char *ret = pchDestination;

 while (*pchDestination != '/0')
 {
  pchDestination++;
 }
 while (count--)
 {
  if (*pchSource != '/0')
  {
   *pchDestination = *pchSource;
   pchSource++;
   pchDestination++;
  }else
  {
   break;
  }
 }   
    *pchDestination = '/0';

 return ret;
}

///

char *MyStrchr(const char *pStr, int find)
{
    if (pStr == NULL)
 {
  return NULL;
 }
 if (find == 0)
 {
  return NULL;
 }

 char *ret = (char *)pStr;

 while (*ret != '/0')
 {
  if (*ret == find)
  {
   return ret;
  }
  ret++;
 }
 return NULL;
}

void *MyMemchr(const void *pStr, int find, size_t count)
{
 if ((count == 0) || (pStr == NULL))
 {
  return NULL;
 }
   
 const unsigned char *p = (const unsigned char *)pStr;//为什么要转换成这种类型呢?

 while (count--)
 {
  if (*p != find)
  {
   p++;
  }else
  {
   return (void *)p;
  }
 }

 return NULL;
}

//

int MyMemcmp(const void *pchStr1, const void *pchStr2, size_t count)
{
 if ((pchStr1 == NULL) || (pchStr2 == NULL))
 {
  printf("StrCompare input error!/n");
  exit(-1);
 }
 if (count == 0)
 {
  return 0;
 }

 int ret = 0;
    const unsigned char *p1 = (const unsigned char *)pchStr1;//比较内存都要转换为最小存储单位
    const unsigned char *p2 = (const unsigned char *)pchStr2;

 while (count--)
 {
  if (*p1 == *p2)
  {
   p1++;
   p2++;
  }else
  {
   ret =  (*p1 - *p2);
  }
 }

 return ret;
}
/

void *MyMemset(void *pchDest, int set, size_t count)
{
 if (pchDest == NULL)
 {
  return NULL;
 }
 if (count == 0)
 {
  return pchDest;
 }

 unsigned char *p = (unsigned char *)pchDest;
 unsigned char s = (unsigned char)set;

 while (count--)
 {
  *p = s;
  p++;
 }

 return pchDest;
}

//

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值