搜索
熱搜: 活動 交友 discuz
查看: 3200|回復: 0
打印 上一主題 下一主題

[教學] 字串連結指令 strcat()、strncat()說明及使用範例

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-8-14 04:38:15 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. 以下的指令說明,是以MSDN上的說明為主
  2. 並適用於VC++的編譯器
  3. 若有不適用於其它C編譯器的地方
  4. 還請其它的高手
  5. 代為修改一下


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

  7. (1)strcat()

  8. 1.指令說明:

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

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

  14. 輸出:
  15. 執行完,後會將strDestination的位置作輸出

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

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

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

  26.         printf("執行strcat()指令後\n");
  27.         printf("szDestination=%s\n", szDestination);
  28.         
  29.         system("pause");
  30. }

  31. 輸出:

  32. 執行strcat()指令前
  33. szDestination=0123
  34. 執行strcat()指令後
  35. szDestination=01234567

  36. 3.實作:

  37. // 實作strcat指令
  38. // strDestination ==> 目的字串指標
  39. // strSource ==> 來源字串指標
  40. char* MyStrcat(char* strDestination, const char* strSource)
  41. {
  42.         int                nLen = 0;
  43.         int                nCount = 0;

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

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

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

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

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

  61.         // 回傳目的字串指標
  62.         return strDestination;
  63. }

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

  67. MSDN對strcat指令的說明



  68. (2)strncat()

  69. 1.指令說明:

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

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

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

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

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

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

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

  97.         printf("執行strncat()指令後\n");
  98.         printf("szDestination=%s\n", szDestination);
  99.         
  100.         system("pause");
  101. }


  102. 輸出:

  103. 執行strncat()指令前
  104. szDestination=0123456
  105. 執行strncat()指令後
  106. szDestination=012345678

  107. 3.實作:

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

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

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

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

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

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

  136.         // 回傳目的字串指標
  137.         return strDestination;
  138. }

  139. MSDN對strncat()指令的說明
複製代碼
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2026-6-4 07:32 , Processed in 0.031109 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表