1. 程式人生 > >字符串函數(strcpy字符串拷,strcmp字符串比較,strstr字符串查找,strDelChar字符串刪除字符,strrev字符串反序,memmove拷貝內存塊,strlen字符串長度)

字符串函數(strcpy字符串拷,strcmp字符串比較,strstr字符串查找,strDelChar字符串刪除字符,strrev字符串反序,memmove拷貝內存塊,strlen字符串長度)

RR size art 刪除字符 rcp 技術分享 count oid space

1.strcpy字符串拷貝
拷貝pStrSource到pStrDest,並返回pStrDest地址(源和目標位置重疊情況除外)

技術分享圖片
char *strcpy(char *pStrDest, const char *pStrSource)
{
    assert(NULL!=pStrDest && NULL!=pStrSource);
    char *strTemp=pStrDest;
    while ((*pStrDest++ = *pStrSource++) != ‘\0‘);

    return strTemp;
}
技術分享圖片

2.strcmp字符串比較

技術分享圖片
int strcmp(const char *pStrA, const char *pStrB)
{
    assert(NULL!=pStrA && NULL!=pStrB);
    while (*pStrA && *pStrB && *pStrA==*pStrB)
    {
        ++pStrA;
        ++pStrB;
    }

    return (pStrA-*pStrB);
}
技術分享圖片

3.strstr字符串查找

技術分享圖片
char *strstr(const char *pStrSource, const char *pStrSearch)   
{   
    assert(pStrSource != NULL && pStrSearch != NULL); 
    const char *strTempSource = pStrSource;
    const char *strTempSearch = pStrSearch;
    for (; *pStrSource!=‘\0‘; ++pStrSource)
    {
        for (strTempSource=pStrSource,strTempSearch=pStrSearch;
        *strTempSearch!=‘\0‘ && *strTempSearch==*strTempSource;
        ++strTempSource, ++strTempSearch);

        if (*strTempSearch == ‘\0‘)
        {
            return (char *)pStrSource;
        }
    }

    return (char *)NULL; 
} 
技術分享圖片

4.strDelChar字符串刪除字符

技術分享圖片
char *strDelChar(char *pStrSource, const char chDel)
{
    assert(NULL!=pStrSource && !isspace(chDel));
    char *pTempStrA, *pTempStrB;
    pTempStrA = pTempStrB = pStrSource;

    while (*pTempStrB++)
    {
        if (*pTempStrB != chDel)
        {
            *pTempStrA++ = *pTempStrB; 
        }
    }
    *pTempStrA = ‘\0‘;

    return pStrSource;
}
技術分享圖片

5.strrev字符串反序

技術分享圖片
char *strrev(char *pStrSource)
{
    assert (NULL != pStrSource);

    char *pStrStart, *pStrEnd;
    pStrStart = pStrEnd = pStrSource;
    while (*pStrEnd != ‘\0‘)
    {
        ++pStrEnd;
    }

    char chTemp;
    for (--pStrEnd, pStrStart; pStrEnd<pStrStart; ++pStrStart, --pStrEnd)
    {
        chTemp = *pStrStart;
        *pStrStart = *pStrEnd;
        *pStrEnd = chTemp;
    }

    return pStrSource;
}
技術分享圖片

6.memmove拷貝內存塊

技術分享圖片
void *memmove(void *pStrTo, const void *pStrFrom, size_t count)
{
    assert (NULL!=pStrTo && NULL!=pStrFrom);

    void *pStrRet = pStrTo;

    if (pStrTo<pStrFrom || pStrTo>pStrFrom+count-1)
    {
        //內存塊不重疊情況
        while (count--)
        {
            *pStrTo++ = *pStrFrom++;
        }
    }
    else
    {
        //內存塊重疊情況
        char *pStrDest = (char *)pStrTo;
        char *pStrSource = (char *)pStrFrom;
        pStrDest = pStrDest+count-1;
        pStrSource = pStrSource+count-1;
        while (count--)
        {
            *pStrDest-- = *pStrSource--;
        }
    }

    return pStrRet;
}
技術分享圖片

7.strlen字符串長度

技術分享圖片
int strlen(const char *pStrSource)
{
    assert(NULL != pStrSource);
    int iLen = 0;
    while (*pStrSource++ != ‘\0‘)
    {
        ++iLen;
    }

    return iLen;
}
技術分享圖片

http://www.cnblogs.com/sz-leez/p/4531507.html

字符串函數(strcpy字符串拷,strcmp字符串比較,strstr字符串查找,strDelChar字符串刪除字符,strrev字符串反序,memmove拷貝內存塊,strlen字符串長度)