cconcatenation

Concatenating command line arguments


I am currently tying to make a function that put all the command lines arguments into a single string while separating them with a '\n'. My current code does work fine but with the only problem being a '\n' it add after the last command line argument.

Here is my current code:

char *concat_parameters(int ac, char **av)
{
    int i = 0;
    int length = 0;
    char *str;

    for (i = 0; i != ac; ++i){
        length = length + my_strlen(av[i]) + 1;
    }
    str = (char *) malloc(sizeof(char) * length);
    for (i = 0; i != ac; ++i){
        if (i == (ac - 1)){
            concat_strings(str, av[i]);
            break;
        } else {
            concat_strings(str, av[i]);
            concat_strings(str, "\n");
        }
    }
    return str;
}

Solution

  • Assuming that your functions my_strlen and concat_strings behave exactly the same way as the functions strlen and strcat from the C standard library, your program has a more serious problem than the undesired newline character.

    The function strcat works by first finding the end of the destination string (which is marked with a null character) and then appending the source string to the destination string. This means that strcat should not be used on a memory buffer that contains uninitialized data, because unless the first character happens to be a null character, you will get uninitialized data in your string, or your program may crash due to strcat overflowing the buffer while trying to find the null character.

    One way to solve this is to use strcpy instead of strcat the first time you write to the destination string. The function strcpy will simply overwrite the destination string and will not search for a null character. However, this solution would be cumbersome in this case, because you wouldn't be able to consistently use strcat.

    A simpler solution would be to mark the destination string as empty, by explicitly writing a null character to the first character of the destination string. That way, you can use strcat on it immediately afterwards and won't need strcpy. You can consistently use strcat on the destination string.

    In order to do this, you can add the following line before the loop:

    str[0] = '\0';
    

    If you want the last line to not have a newline character at the end, then one thing you can do is to add the newline character in the next loop iteration instead of the current one, before adding the next string. This means that all loop iterations except the first one should add the newline character. You can accomplish this with the following code:

    for ( i = 0; i != ac; i++ )
    {
        // add a newline character unless we are in the first iteration
        if ( i != 0 )
        {
            strcat( str, "\n" );
        }
    
        // concatenate new string too existing string
        strcat( str, av[i] );
    }
    

    A more efficient, but less readable solution is to use an infinite loop and to move the loop condition inside the loop:

    for ( i = 0; ; )
    {
        // add new string to existing string
        strcat( str, av[i] );
    
        // break out of infinite loop if loop termination condition
        // is reached
        if ( ++i == ac )
        {
            break;
        }
    
        // add newline character
        strcat( str, "\n" );
    }
    

    That way, you only have to perform one comparison instead of two comparisons per loop iteration.

    Note however that calling strcat in a loop is highly inefficient, because every time you call strcat, that function must scan the entire string to find the end of the string. This is known as Shlemiel the painter’s algorithm, and this algorithm has a time complexity of O(n^2). If you only have a few loop iterations and the strings are short, then this probably won't be a problem. But if you have many loop iterations and large strings, then this could be a serious problem.

    Therefore, it would be more efficient not to use strcat at all, but to instead keep track of the end of the destination string yourself, and to write data to the end using memcpy, for example like this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *concat_parameters( int ac, char **av )
    {
        int length = 0;
    
        // count total length of strings
        for ( int i = 0; i < ac; i++ )
        {
            length = length + strlen( av[i] ) + 1;
        }
    
        // allocate memory for destination string
        char *str = malloc( length );
    
        // add variable to keep track of the end of the
        // destination string
        char *end = &str[0];
    
        for ( int i = 0; ; )
        {
            // calculate length of string to be appended
            length = strlen( av[i] );
    
            // add new string to existing string
            memcpy( end, av[i], length );
    
            // update pointer to point to new end
            end += length;
    
            // break out of infinite loop if loop termination condition
            // is reached
            if ( ++i == ac )
            {
                break;
            }
    
            // add newline character
            *end++ = '\n';
        }
    
        //  write null character to the end of the destination string
        *end = '\0';
    
        return str;
    }
    
    int main()
    {
        char *test[] = { "Apples", "Bananas", "Oranges" };
        char *result = concat_parameters( sizeof test / sizeof *test, test );
        printf( "%s\n", result );
        free( result );
    }
    

    This program has the following output:

    Apples
    Bananas
    Oranges