close

字串連結指令 strcat()、strncat()

(1)strcat()

1.指令說明:

原型為:
char *strcat(char *strDestination, const char *strSource);

參數說明:
strDestination: 字串連接後,要存放的位置(目的地的指標)
strSource: 字串連接的來源位置(來源的指標)

輸出:

執行完,後會將strDestination的位置作輸出

2.使用範例:
ex.
void main()
{
        char        szDestination[10] = "0123";
        char        szSource[] = "4567";

        printf("執行strcat()指令前\n");
        printf("szDestination=%s\n", szDestination);

        // 將szSource字串連接到szDestination字串之後
        strcat(szDestination, szSource);

        printf("執行strcat()指令後\n");
        printf("szDestination=%s\n", szDestination);
        
        system("pause");
}

輸出:

執行strcat()指令前
szDestination=0123
執行strcat()指令後
szDestination=01234567

3.實作:

// 實作strcat指令
// strDestination ==> 目的字串指標
// strSource ==> 來源字串指標
char* MyStrcat(char* strDestination, const char* strSource)
{
        int                nLen = 0;
        int                nCount = 0;

        // 這一個地方,是防止strDestination或strSource是指向NULL,所作的錯誤偵測
        if(strDestination == NULL || strSource == NULL)
        {
                return NULL;
        }

        // 計算要在strDestination陣列中,開始連接strSource字串的位置
        nLen = strlen(strDestination);

        // 若來源字串指標未讀到字串結尾'\0'
        while(*(strSource + nCount) != '\0')
        {
                // 將目前strSource + nCount所指的字元,放到strDestination中(目的字串指標)
                *(strDestination + nLen + nCount) = *(strSource + nCount);

                // 指向下一個要連接的位置
                nCount++;
        }

        // 在strDestination的中,放入字串結尾'\0'
        *(strDestination + nLen + nCount) = '\0';

        // 回傳目的字串指標
        return strDestination;
}

ps:
strncat()指令在執行時,並無法考慮到目的指標陣列,是否有足夠的空間來存放連接後的字串
因此很容易讓陣列爆掉(Buffer Overow),因此建議使用strncat()來取代strcat()

[url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strcat.2c_.wcscat.2c_._mbscat.asp] MSDN對strcat指令的說明[/url]



(2)strncat()

1.指令說明:

原型為:
char *strncat(char *strDest, const char *strSource, size_t count);

參數說明:
strDest: 字串複製後要存放的位置(目的地的指標)
strSource: 字串複製的來源位置(來源的指標)
count:最多可從strSource取得多少字元,來連接到strDest

輸出:
執行完,後會將strDest的位置作輸出

2.使用範例:
ex.
void main()
{
        // 強列建議在宣告szDestination前,要先作陣列初使化或在字串陣列的最後一個字元
        // 放入'\0',防止szSource的字元數,比szDestination還大時
        // 在執行完strncat指令後,會因找不到字串結尾,而引起不必要的錯誤
        char        szDestination[10] = "0123456";
        char        szSource[] = "789ABCDEF";
        int        nCount = 0;

        printf("執行strncat()指令前\n");
        printf("szDestination=%s\n", szDestination);

        // sizeof(szDestination) ==> 計算szDestination陣列大小
        // strlen(szDestination) ==> 計算szDestination陣列中,存放的字元數
        // sizeof(szDestination) - strlen(szDestination) ==> szDestination陣列中,剩少多少空間,可存放字元
        nCount = sizeof(szDestination) - strlen(szDestination);

        // 將szSource字串連接到szDestination字串之後
        // nCount - 1中的 -1是為了要預留字串結尾'\0'的空間
        strncat(szDestination, szSource, nCount - 1);

        printf("執行strncat()指令後\n");
        printf("szDestination=%s\n", szDestination);
        
        system("pause");
}


輸出:

執行strncat()指令前
szDestination=0123456
執行strncat()指令後
szDestination=012345678

3.實作:

// 實作strncat指令
// strDestination ==> 目的字串指標
// strSource ==> 來源字串指標
char* MyStrncat(char* strDestination, const char* strSource, unsigned int count)
{
        int                nLen = 0;
        int                nCount = 0;

        // 這一個地方,是防止strDestination或strSource是指向NULL,所作的錯誤偵測
        if(strDestination == NULL || strSource == NULL)
        {
                return NULL;
        }

        // 計算要在strDestination陣列中,開始連接strSource字串的位置
        nLen = strlen(strDestination);

        // 若來源字串指標未讀到字串結尾'\0'
        // 並且所連接的字元數,不得大於count
        while(*(strSource + nCount) != '\0' && nCount < count)
        {
                // 將目前strSource + nCount所指的字元,放到strDestination中(目的字串指標)
                *(strDestination + nLen + nCount) = *(strSource + nCount);

                // 指向下一個要連接的位置
                nCount++;
        }

        if(*(strSource + nCount) == '\0')
        {
                // 在strDestination的中,放入字串結尾'\0'
                *(strDestination + nLen + nCount) = '\0';
        }

        // 回傳目的字串指標
        return strDestination;
}

[url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strncat.2c_.wcsncat.2c_._mbsncat.asp] MSDN對strncat()指令的說明[/url]

其它字串複製的指令,尚有
char *_strdup( const char *strSource );
等不常用的指令
請自行參閱MSDN的說明

[url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wcecrt4/html/erlrfusstrdupcmauswcsdup.asp] MSDN對strdup指令的說明[/url]


Reference

http://www.dk101.com/Discuz/viewthread.php?tid=25940


arrow
arrow
    全站熱搜

    Chris701203 發表在 痞客邦 留言(0) 人氣()