C: Concatenation of 'n' strings

user300045

How to concatenate multiple strings in C? I have a function for concatenation of two strings (no strcat()):

char* concat(char *s1, char *s2)
{
    char *r, *t;
    int d1 = -1, d2 = -1;
    while (s1[++d1]);
    while (s2[++d2]);
    t = r = (char *)calloc(d1 + d2 + 1, sizeof(char));
    while (*t++ = *s1++);
    t--;
    while (*t++ = *s2++);
    return r;
}

Is there a way to use this function (or strcat()) to concatenate multiple strings? Also, is this dynamic allocation right:

char* concat(char** array, int n)
{
    char *r; int i;
    r=(char *)calloc(n*MAX+1, sizeof(char));
    array=(char **)malloc(n * sizeof(char *));
    for(i=0;i<n;i++)
    {
        array[i]=(char *)malloc(MAX+1);
    }    
   ... //concatenation//...
    free(r);
    free(array[i]);
    free(array);
    return r;
}
Sergey Kalinichenko

Yes, you can extend the code from the first function to deal with a whole array:

char* concat(char** array, size_t n) {
    size_t total = 1; // One for null terminator
    for (int i = 0 ; i != n ; i++) {
        total += strlen(array[i]);
    }
    char *res = malloc(total);
    char *wr = res;
    for (int i = 0 ; i != n ; i++) {
        char *rd = array[i];
        while ((*wr++ = *rd++))
            ;
        wr--;
    }
    *wr = '\0';
    return res;
}

You do not need to allocate temporary 2D structures. Since you can precompute the length of the result, a single allocation is sufficient.

Demo.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

bitxor on c++ strings

분류에서Dev

Randomizing an array of C strings

분류에서Dev

comparing and defining strings in C

분류에서Dev

Reading and printing strings in C

분류에서Dev

c string manipulation for incoming strings

분류에서Dev

Allocating memory to array of strings in c

분류에서Dev

Making sequential strings in c programming

분류에서Dev

C# Compare strings with umlaut strange results

분류에서Dev

C parsing strings into separate variables without delimiters?

분류에서Dev

Using sscanf to read strings with white spaces in C

분류에서Dev

Is it possible to compare strings from the same variable in C?

분류에서Dev

C - Array of Strings & Mysterious Valgrind Error

분류에서Dev

concatenation of arrays in system verilog

분류에서Dev

sybase - simple concatenation

분류에서Dev

String initialisation and concatenation in java

분류에서Dev

Simplify CTE string concatenation?

분류에서Dev

argument concatenation in a recursive function

분류에서Dev

String concatenation in Access VBA

분류에서Dev

SQL WHERE clause with concatenation

분류에서Dev

string concatenation and apostrophe marks

분류에서Dev

Ruby on Rails String Concatenation

분류에서Dev

Powershell string concatenation of expressions

분류에서Dev

concatenation to previous line in bash

분류에서Dev

Why are strings' space complexity O(n) but numbers are O(1)?

분류에서Dev

Split string into string list containing strings no greater than n chars

분류에서Dev

Matlab string concatenation for file creation

분류에서Dev

Keras: output of concatenation has no parameters?

분류에서Dev

Java 8 Vavr onFailure concatenation

분류에서Dev

using concatenation in getUrl during loop?