雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C字符串操作函數(shù)實(shí)現(xiàn)方法小結(jié)

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊:

本文實(shí)例講述了C字符串操作函數(shù)實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:

下面是部分C字符串操作函數(shù)的實(shí)現(xiàn),或許在某些筆試時(shí)可以用到!

#ifndef NULL
#define NULL ((void *)0)
#endif
/*
  memcpy的實(shí)現(xiàn)代碼
*/
void* memcpy(void *pDst, void *pSrc, int iLen)
{
  char *pTmp = (char *)pDst;
  char *pTmp2 = (char *)pSrc;
  if(0 == iLen)
    return pDst;
  while(iLen--)
    *pTmp++ = *pTmp2++;
  return pDst;
}
/*
  memset的實(shí)現(xiàn)代碼
*/
void* memset(void *pDst, int iSet, int iLen)
{
  char *pTmp = (char *)pDst;
  if(0 == iLen)
    return pDst;
  while(iLen--)
    *pTmp++ = (char)iSet;
  return pDst;
}
/*
  strcpy的實(shí)現(xiàn)代碼
*/
char *strcpy(char *pDst, char *pSrc)
{
  char *pRst = pDst;
  do
    *pDst++ = *pSrc;
  while(*pSrc++);
  return pRst;
}
/*
  strcat的實(shí)現(xiàn)代碼
*/
char *strcat(char *s, char *a)
{
  char *save = s;
  for(; *s; ++s);
  while((*s++ = *a++) != 0);
  return save;
}
/*
  strlen的實(shí)現(xiàn)代碼
*/
int strlen(char *pStr)
{
  int iLen = 0;
  while(*pStr++)
    iLen++;
  return iLen;
}
/*
  strcmp的實(shí)現(xiàn)
*/
int strcmp(char *s, char *t)
{
  for(; *s == *t; s++, t++)
  {
    if(('/0' == *s) || ('/0' == *t))
    {
      if(*s == *t)
        return 0;
      else
        break;
    }
  }
  return ((*s > *t) ? 1 : -1);
}
/*
  strncmp的實(shí)現(xiàn)
*/
int m_strncmp(char *s, char *t, int n)
{
  if(0 == n)
    return 0;
  for (; (--n > 0) && (*s==*t); s++,t++)
  {
    if ('/0'==*s)
      return 0;
  }
  if(*s == *t)
    return 0;
  return ((*s > *t) ? 1 : -1);
}
/*
  strstr的實(shí)現(xiàn)
*/
char* strstr(char *s, char *find)
{
  char c, sc;
  unsigned int len;
  if ((c = *find++) != 0) 
  {
    len = lzs_strlen(find);
    do 
    {
      do 
      {
        if ((sc = *s++) == 0)
          return (NULL);
      } while (sc != c);
    } while (lzs_strncmp(s, find, len) != 0);
    s--;
  }
  return ((char *)s);
}

希望本文所述對(duì)大家的C語(yǔ)言程序設(shè)計(jì)有所幫助。

上一篇:C++基于棧實(shí)現(xiàn)鐵軌問(wèn)題

欄    目:C語(yǔ)言

下一篇:C++非遞歸建立二叉樹(shù)實(shí)例

本文標(biāo)題:C字符串操作函數(shù)實(shí)現(xiàn)方法小結(jié)

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3096.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有