2014-04-17 162 views
1

我的任務是編寫一個函數,該函數的參數爲​​char**並返回char*。 char *必須指向一個以null結尾的字符串,它是char **中每個字符串的串聯。從C中的字符串數組創建字符串

我該如何解決這個問題?
一些地方建議循環使用char**並使用strcat()來構建字符串,但strcat()要求我有一個足夠容納最終字符串的目標;我不知道弦會有多大。我是否應該通過詢問char**的每個元素的strlen()來找出它的大小?

在C中做到這一點的最好方法是什麼?

回答

3

可能的方法:

  • 遍歷char**,積累的strlen(char*)每個條目的結果。
  • 添加一個到終止空字符的累計長度。
  • 使用malloc()爲字符串分配內存,並將第一個字符設置爲空終止符(以確保strcat()第一次正確運行)。
  • 使用strcat()再次迭代。

調用者負責返回的字符串的文檔free()

2

主要有兩種方法可以做到這一點:

  • 做主持的所有字符串的初始趟次,並完成所有的strlen小號
  • 開始的總和與NULL串並遍歷字符串。當你重複你realloc目標,以適應規模的增加
0

是的,你可要來計算所有需要的,所以你可以爲新的字符串的malloc足夠的空間的空間。

的所有字符串創建空間後,只需用一個空格字符複製它們也許他們之間(但如果你添加空格,不要忘了算他們的分配...)

0

您可能需要考慮以下注釋代碼(live here on Ideone.com)。

總結:

  1. 迭代通過輸入字符串數組,計算所得到的串的總長度,輸入字符串的長度求和,使用strlen()
  2. 使用malloc()爲結果字符串分配內存。
  3. 使用strcat()重新迭代輸入數組,連接輸入字符串。

#include <stdio.h>  /* For printf()    */ 
#include <stdlib.h>  /* For malloc(), free()  */ 
#include <string.h>  /* For strcat(), strlen() */ 


/* 
* Concatenates input strings into a single string. 
* Returns the pointer to the resulting string. 
* The string memory is allocated with malloc(), 
* so the caller must release it using free(). 
* The input string array must have NULL as last element. 
* If the input string array pointer is NULL, 
* NULL is returned. 
* On memory allocation error, NULL is returned. 
*/ 
char * ConcatenateStrings(const char** strings) 
{ 
    int i = 0;    /* Loop index    */ 
    int count = 0;   /* Count of input strings */ 
    char * result = NULL; /* Result string   */ 
    int totalLength = 0; /* Length of result string */ 


    /* Check special case of NULL input pointer. */ 
    if (strings == NULL) 
    { 
     return NULL; 
    } 

    /* 
    * Iterate through the input string array, 
    * calculating total required length for destination string. 
    * Get the total string count, too. 
    */ 
    while (strings[i] != NULL) 
    { 
     totalLength += strlen(strings[i]); 
     i++; 
    } 
    count = i; 
    totalLength++; /* Consider NUL terminator. */ 

    /* 
    * Allocate memory for the destination string. 
    */ 
    result = malloc(sizeof(char) * totalLength); 
    if (result == NULL) 
    { 
     /* Memory allocation failed. */ 
     return NULL; 
    } 

    /* 
    * Concatenate the input strings. 
    */ 
    for (i = 0; i < count; i++) 
    { 
     strcat(result, strings[i]); 
    } 

    return result; 
} 

/* 
* Tests the string concatenation function. 
*/ 
int main(void) 
{ 
    /* Result of string concatenation */ 
    char * result = NULL; 

    /* Some test string array */ 
    const char * test[] = 
    { 
     "Hello ", 
     "world", 
     "!", 
     " ", 
     "Ciao ", 
     "mondo!", 
     NULL    /* String array terminator */ 
    }; 

    /* Try string concatenation code. */ 
    result = ConcatenateStrings(test); 

    /* Print result. */ 
    printf("%s\n", result); 

    /* Release memory allocated by the concatenate function. */ 
    free(result); 

    /* All right */ 
    return 0; 
}