Concatenating array of strings into one string separated by spaces

Hi,

Well as the title says, I have an array of strings (delimited by null). The length of the array is variable and length of each string is variable as well. What I need is one huge string with the original strings in the array separated by spaces.

For example is an array is such that array[0] = Firstname, array[1] = Secondname, I want to be able to extract that and store it in a char * which would then be "firstname Secondname".

Please tell how I would go about doing this.

Thanks.

Hi,

So I figured out how to do it. In case, anybody else are looking for the same thing, I am posting my version here:

/*                                                                            
 * Subroutine to extract the full name by concatenating                       
 * array of strings into one full string separated by spaces.                 
 *                                                                            
 * Input: number of fields and array of strings                               
 * Returns: full string name (char *)                                         
 */                                                                           
                                                                              
char *get_full_name(int n, char **f){                                         
    int tot_length = 1, i;                                                    
    char *p, *result;                                                         
    const char *current;                                                      
    for (i = 1; i < n; i++){                                                  
        tot_length += strlen(f) + 1;                                       
    }                                                                         
    result = (char *) malloc ((unsigned) tot_length);                         
    for (p = result, i = 1; i < n; i++){                                      
        current = f;                                                       
        if (strlen(current) == 0){                                            
            continue;                                                         
        }                                                                     
        memcpy(p, current, (size_t) strlen(current));                         
        p += strlen(current);                                                 
        *p = ' ';                                                             
        p++;                                                                  
    }                                                                         
    return (result);                                                          
}                                                                             

I am not sure whether it is the perfect way to do it..but it works :).

Thanks.

I can see a couple of problems already. You are assuming arrays in C index from 1 instead of 0. You are adding a trailing space to your result string. You are not adding a '\0' to the end of your result string.

Hi,

Thanks for your comments. The index problem is actually not a problem because this function is part of a program I am writing and I know that whatever I need will start from index 1 (because I am passing it that way). That can be changed here.

Could I just add a null at the end of result like say:

result--;
*result = '\0';

Although this didnt work for me.

Yes, that should work.

Here is a version of your code which eliminates (a) 2 calls to strlen() and (b) the need to pass the number of array elements to get_full_name().

#include <stdio.h>
#include <string.h>

char *
get_full_name(char *f[])
{
    int tot_length = 1, i, len;
    char *p, *result;

    for ( i = 0; f != NULL; i++)
        tot_length += strlen(f) + 1;

    result = (char *)malloc((unsigned) tot_length);

    for (p = result, i = 0; f != NULL; i++)
    {
        if (!(len = strlen(f)))
            continue;
        memcpy(p, f, (size_t)len);
        p += len;
        *p++ = ' ';
    }

    if (p > result)
       --p;
    *p='\0';

    return (result);
}

int
main(int argc, char **argv)
{
    char *namestr[] = {"John", "Quincy", "Adams", NULL};
    char *result;

    result = get_full_name(namestr);

    printf("Result: <%s>\n", result);

    exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *get_full_name(const char **t)
{
    char *f=NULL;
    
    while (*t) {
        f = (char *) realloc(f, strlen(*t)+strlen(f)+1);
        sprintf(f,"%s%s%s",f,(strlen(f)?" ":""),*t++);
    }
    return f;
}

int main(int argc, char **argv)
{
    const char *str[] = {"John", "Quincy", "Adams", NULL};
    char *res;

    res = get_full_name(str);
    printf("Result: <%s>\n", res);
    return 0;
}

Hi,

Thats interesting. But it segfaults for me, when it calls to strlen.

Thanks.

Works fine for me...can you post the stack trace.
What platform are you on?

Hi,

Um..I am relatively new to programming in linux, how do I get a stack trace?

Sorry.

Don't know why it would crash on Linux (works fine on AIX) and you say it's strlen that's causing the crash. To get a stack trace do the following and post the output...

gcc -g file.c
./a.out
gdb ./a.out core
(gdb) bt

Ok I did that. It just segfaluted..no core was dumped. And when tried bt it just said "No stack".

Am I doing something wrong?

Thanks.

No core file? that's strange. What compiler and version you got? Maybe the compiler on Linux doesn't like that f is set to NULL so give the code below a try...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *get_full_name(const char **t)
{
    char *f=(char *) malloc(sizeof(char));
    
    while (*t) {
        f = (char *) realloc(f, strlen(*t)+strlen(f)+1);
        sprintf(f,"%s%s%s",f,(strlen(f)?" ":""),*t++);
    }
    return f;
}

int main(int argc, char **argv)
{
    const char *str[] = {"John", "Quincy", "Adams", NULL};
    char *res;

    res = get_full_name(str);
    printf("Result: <%s>\n", res);
    return 0;
}