發新話題

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

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

以下的指令說明,是以MSDN上的說明為主
並適用於VC++的編譯器
若有不適用於其它C編譯器的地方
還請其它的高手
代為修改一下
複製內容到剪貼板
代碼:
字串連結指令 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]
[ 本帖最後由 philxyz0316 於 2006-8-27 13:05 編輯 ]

TOP

複製內容到剪貼板
代碼:
char* TryStrnCopy(char* strDestination,const char* strSource,int nSizeMax){
  char* strTemp=strDestination;  
  int count=0;
  if(strDestination==NULL||strSource==NULL)
    return NULL;   
  while(*strDestination++=*strSource++)
   if((count++>=nSizeMax)&&!(*(strDestination-1)='\0'))
      break;
  return strTemp;   
}
複製內容到剪貼板
代碼:
char* TryStrCopy(char* strDestination,const char* strSource){
  char* strTemp=strDestination;  
  if(strDestination==NULL||strSource==NULL)
    return NULL;  
  while(*strDestination++=*strSource++)
    ;
  return strTemp;  
}

TOP

複製內容到剪貼板
代碼:
函式原型char* strstr(char* strDestination,const char* strSource)
參數strDestination是要被搜尋的字串
參數strSource是所要搜尋的字串
回傳為搜尋到的字串位置

範例:
  char a[]="ABCDEFG";
  char b[]="CD";
  printf("%s",strstr(a,b));

輸出結果為
  CDEFG
  //因為有找到"CD"這個字串,所以回傳'C'那個字元的位置,若再以字串印出就是從'C'印到這個字串的結尾也就是'G'

實做:
char* TryStrStr(char* strDestination,const char* strSource){
  char* strTemp=strDestination,*strDeSt=strDestination;
  const char* strSoSt=strSource;
  
  if(strDestination==NULL||strSource==NULL)
    return NULL;  
  while(*strDestination){
    if(*strSoSt==*strDestination){
      while(*strSoSt++==*strDeSt++){
        if(*strSoSt=='\0')
          return strDestination;              
      }   
      strSoSt=strSource;
      strDeSt=strDestination;   
    }
    strDestination++;
    strDeSt=strDestination;
  }
  return NULL;  
}

TOP

使用strstr()指令時
要小心當找不到字串時
會回傳NULL
因此在程式中
要有一個判斷找不到字串的地方

以下是判斷的範例
複製內容到剪貼板
代碼:
void main()
{
        char*        p = NULL;
        char        szTemp[] = "ABCDEFG";
        char        szSearch[] = "EF";

        // 在szTemp陣列中,尋找字串szSearch
        // 並輸出找到的位置
        p = strstr(szTemp, szSearch);

        // 若找到了該字串
        if(p != NULL)
        {
                // 這裡處理找到字串時的狀況

                printf("\n在字串%s中 字串%s 的位置為:%d\n", szTemp, szSearch, (p - &szTemp[0]));
        }
        else // 找不到字串
        {
                // 這裡處理找不到字串時的狀況

                printf("\n在字串%s中 找不到字串%s\n", szTemp, szSearch);
        }

        system("pause");
}


輸出:

在字串ABCDEFG中 字串EF 的位置為:4
ps:以上的位置從0開始,因此位置4

TOP

真的是太謝謝了
真的很詳細
感謝分享呢

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。