Concatenating command line arguments [closed]

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;
}

14

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 strpy 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] = '';

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 = '';

    return str;
}

int main()
{
    char *test[] = { "Apples", "Bananas", "Oranges" };
    char *result = concat_parameters( sizeof test / sizeof *test, test );
    printf( "%sn", result );
    free( result );
}

This program has the following output:

Apples
Bananas
Oranges

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật