C语言基础字符串函数源代码

#include 
#include 

/*
 * 作用:获取串str的长度
*/
int strlen(const char *pcStr)
{
    assert(pcStr != NULL);

    int iLen = 0;

    while ('\0' != *pcStr)
    {
        pcStr++;
        iLen++;
    }

    return iLen;
}

/*
 * 前提:strDes指向的内存至少能够容纳(strlen(strDes) + strlen(strSrc) + 1) bytes
 * 作用:将串strSrc连接到串strDes的末尾,函数本身不保证安全(使用不当会内存溢出)
*/
char *strcat(char *pcStrDest, const char *pcStrSrc)
{
    assert((pcStrDest != NULL) && (pcStrSrc != NULL));

    char *pcTemp = pcStrDest;

    /* pcStrDes指针指向串pcStrDes的末尾 */
    while ('\0' != *pcStrDest)
    {
        pcStrDest++;
    }

    /* 依次从源串pcStrSrc不断拷贝字符到目的串pcStrDes中 */
    while ('\0' != *pcStrSrc)
    {
        *pcStrDest = *pcStrSrc;

        pcStrSrc++;
        pcStrDest++;
    }

    *pcStrDest = *pcStrSrc;

    return pcTemp;
}

/*
 * 前提:strDes指向的内存至少能够容纳(strlen(strSrc) + 1) bytes
 * 作用:将串strSrc拷贝到strDes指向的内存空间,函数本身不保证安全(使用不当会内存溢出)
*/
char *strcpy(char *pcStrDest, const char *pcStrSrc)
{
    assert((pcStrDest != NULL) && (pcStrSrc != NULL));

    char *pcTemp = pcStrDest;

    while ('\0' != *pcStrSrc)
    {
        *pcStrDest = *pcStrSrc;

        pcStrSrc++;
        pcStrDest++;
    }

    *pcStrDest = *pcStrSrc;

    return pcTemp;
}

/*
 * 作用:按字典序比较串s和串t的大小
*/
int strcmp(const char *pcStrA, const char *pcStrB)
{
    assert((pcStrA != NULL) && (pcStrB != NULL));

    while (('\0' != *pcStrA) && ('\0' != *pcStrB) && (*pcStrA == *pcStrB))
    {
        pcStrA++;
        pcStrB++;
    }

    return (int)(*pcStrA - *pcStrB);
}

/*
 * 前提:strDes指向的内存至少能够容纳(strlen(strDes) + count + 1) bytes
 * 作用:将串strSrc的前count个字符连接到串strDes的末尾,函数本身不保证安全(使用不当会内存溢出)
*/
char *strncat(char *pcStrDest, const char *pcStrSrc, int iCount)
{
    assert((pcStrDest != NULL) && (pcStrSrc != NULL));

    char *pcTemp = pcStrDest;

    while ('\0' != *pcStrDest)
    {
        pcStrDest++;
    }

    while ((iCount > 0) && ('\0' != *pcStrSrc))
    {
        *pcStrDest = *pcStrSrc;

        pcStrSrc++;
        pcStrDest++;
        iCount--;
    }

    *pcStrDest = '\0';

    return pcTemp;
}

/*
 * 前提:strDes指向的内存至少能够容纳(count + 1) bytes
 * 作用:将串strSrc的前count个字符拷贝到strDes指向的内存空间,函数本身不保证安全(使用不当会内存溢出)
*/
char *strncpy(char *pcStrDest, const char *pcStrSrc, int iCount)
{
    assert((pcStrDest != NULL) && (pcStrSrc != NULL));

    char *pcTemp = pcStrDest;

    while ((iCount > 0) && ('\0' != pcStrSrc))
    {
        *pcStrDest = *pcStrSrc;

        pcStrSrc++;
        pcStrDest++;
        iCount--;
    }

    return pcTemp;
}

/*
 * 作用:按字典序比较串s和串t的前count个字符的大小
*/
int strncmp(const char *pcStrA, const char *pcStrB, int iCount)
{
    assert((pcStrA != NULL) && (pcStrB != NULL));

    while (('\0' != *pcStrA) && ('\0' != *pcStrB) && (*pcStrA == *pcStrB) && 
           (iCount > 0))
    {
        iCount--;
        if (0 == iCount)
        {
            break;
        }

        pcStrA++;
        pcStrB++;
    }

    return (int)(*pcStrA - *pcStrB);
}

/*
 * 前提:dest指向的内存能够容纳count bytes
 * 作用:将src所指向内存的count bytes拷贝到的dest指向的内存
*/
void *memcpy(void *pvDest, const void *pvSrc, int iCount)
{
    assert((pvDest != NULL) && (pvSrc != NULL));

    void *pvTemp = pvDest;
    char *pcDest = (char *)pvDest;
    const char *pcSrc = (const char *)pvSrc;

    while (iCount > 0)
    {
        *pcDest = *pcSrc;

        pcDest++;
        pcSrc++;
        iCount--;
    }

    return pvTemp;
}

/*
 * 作用:将str的前count个字节置为字符c
*/
void *memset(void *pvSrc, int iCh, int iCount)
{
    assert(pvSrc != NULL);

    char *pcSrc = (char *)pvSrc;

    while (iCount > 0)
    {
        *pcSrc = (char)iCh;

        pcSrc++;
        iCount--;
    }

    return pvSrc;
}

/*
 *  作用:将src所指向内存的count bytes拷贝到的dest指向的内存(解决了memcpy未解决的内存重叠问题)
*/
void *memmove(void *pvDest, const void *pvSrc, int iCount)
{
    assert((pvDest != NULL) && (pvSrc != NULL));

    void *pvTemp = pvDest;
    char *pcDest = (char *)pvDest;
    const char *pcSrc = (const char *)pvSrc;

    // 逻辑:
    // 1)在src小于dest前提下,然后判断src+count指向的内存是否与dest指向的内存重叠
    // 2) 如果重叠则从src+count开始复制,复制到dest+count,从后往前拷贝count字节
    if ((pvSrc < pvDest) && (pvSrc + iCount > pvDest))
    {
        pcDest = pcDest + iCount - 1;
        pcSrc = pcSrc + iCount - 1;

        while (iCount > 0)
        {
            *pcDest = *pcSrc;

            pcDest--;
            pcSrc--;
            iCount--;
        }
    }
    else
    {
        while (iCount > 0)
        {
            *pcDest = *pcSrc;

            *pcDest++;
            *pcSrc++;
            iCount--;
        }
    }

    return pvTemp;
}

int main()
{
    int iLen = 0;
    int iRet = 0;
    char acBuffer[4] = {0};
    char *pcTestStr1 = "Today is sunday.";
    char acTestStr2[] = "Today";
    char acTestStr3[] = "Today is sunday.";
    char acTestStr4[sizeof("Today is ") + sizeof("sunday.") - 1] = "Today is ";

    iLen = strlen(pcTestStr1);

    printf("iLen = %d\n", iLen);

    (void)strncpy(acBuffer, pcTestStr1, sizeof(acBuffer) - 1);

    printf("acBuffer:%s\n", acBuffer);

    iRet = strncmp(pcTestStr1, acTestStr2, strlen(acTestStr2));

    printf("strncmp iRet = %d\n", iRet);

    iRet = strcmp(pcTestStr1, acTestStr2);

    printf("strcmp iRet = %d\n", iRet);

    (void)memmove(&acTestStr3[1], &acTestStr3[0], strlen("Today"));

    printf("memmove acTestStr3:%s\n", acTestStr3);

    printf("before strncat acTestStr4:%s\n", acTestStr4);

    (void)strncat(acTestStr4, "sunday.111", 
                  sizeof(acTestStr4) - strlen(acTestStr4) - 1);

    printf("after strncat acTestStr4:%s\n", acTestStr4);

    return 0;
}

网页标题:C语言基础字符串函数源代码
标题链接:http://ybzwz.com/article/jpdpph.html