Jaykch
Jaykch

Reputation: 103

Trying to concatenate array of c-strings with delimiter

Here is my code

int main(int argc, char *argv[]) {

    char const *strings[10] = {"dhh", "aci", "cdh"};
    join_def(strings, 'l');

    return EXIT_SUCCESS;
}


// part 1 read lines
void join_def(char const **strings, char delim) {
    char *t = new char[100];
    //int length = 0;
    t[0] = '\0';
    int x = sizeof(strings);
    std::cout << delim << std::endl;

    for (int i = 0; i < x; i++) {
        int size = 0;
        while(strings[i][size]!='\0'){
            size++;
            std::cout << strings[i][size] << std::endl;
        }
    }
}

I have spent hours now I just can't get to concatenate it For this task, I cannot use cstring or anything other than iostream so please don't suggest.

The output needs to be a c-string = "dhhlacilcdh"

Upvotes: 2

Views: 488

Answers (3)

Adrian Mole
Adrian Mole

Reputation: 51835

First, you cannot determine the number of elements in an array passed to a function, as that array will decay to a simple pointer. So, your sizeof(strings) expression will evaluate (at compile time) to the (fixed) size, in bytes, of a pointer. For the function to be 'aware' of how many elements there are in an array, it needs to be explicitly told (by way of an extra argument).

Second, you have your i' and size indexes the wrong way round in the std::cout << strings[i][size] << std::endl; line and, further, you increment size before printing the relevant character, whereas it should be incremented after you've printed it.

The code below also does the actual concatenation of the strings, and the modified join_def function now returns a pointer to that result (which must be freed when you're finished with it);

#include <iostream>

char* join_def(char const** strings, char delim, int x)
{
    char* t = new char[100];
    int length = 0;
    t[0] = '\0';
    //int x = sizeof(strings);
    std::cout << delim << std::endl;
    for (int i = 0; i < x; i++) {
        int size = 0;
        while (strings[i][size] != '\0') {
            std::cout << strings[i][size] << std::endl;
            t[length++] = strings[i][size]; // Append this character
            size++;
        }
        t[length++] = delim; // Append delimiter
    }
    t[length] = '\0'; // Append nul-terminator
    return t;
}

int main()
{
    char const* strings[10] = { "dhh", "aci", "cdh" };
    char* result = join_def(strings, 'l', 3);
    std::cout << result << std::endl;
    free(result);
    return 0;
}

Note, also, that I have moved the join_def function code to before the main (which calls it). If you don't do this, then will at least have to provide a (forward) declaration of that function before main (just a char* join_def(char const** strings, char delim, int x); on its own will do).

Feel free to ask for further clarification and/or explanation.

Upvotes: 1

yaodav
yaodav

Reputation: 1256

this is what you are looking for? look that I remove all the std::endl because it like '\n' also i moved your size++ after the std::cout

#include <iostream>


// part 1 read lines
void join_def(char const **strings, char delim,int length) {
    char *t = new char[100];
    //int length = 0;
    t[0] = '\0';
    int x = length;
   

    for (int i = 0; i < x; i++) {
        int size = 0;
        while(strings[i][size]!='\0'){
            std::cout << strings[i][size]; //<--print befure increment size
            size++;
        }
         std::cout << delim;
    }
}




int main(int argc, char *argv[]) {

    char const *strings[] = {"dhh", "aci", "cdh"};
    join_def(strings,'|',3); //<- need to send the length of the char* array

    return EXIT_SUCCESS;
}

Upvotes: 0

UrHyper
UrHyper

Reputation: 93

I'm not exactly sure what you're trying to do, but maybe this helps?

#include <iostream>

// part 1 read lines
void join_def(char const **strings, char delim)
{
    char *t = new char[100];
    //int length = 0;
    t[0] = '\0';

    int x = 0;
    for (int i = 0; strings[i] != nullptr; i++)
        x += sizeof(strings[i]) - 1;
    
    std::cout << delim << std::endl;

    for (int i = 0; strings[i] != nullptr; i++)
    {
        int size = 0;
        while (strings[i][size] != '\0')
        {
            size++;
            std::cout << strings[i][size] << std::endl;
        }
    }
}

int main(int argc, char *argv[])
{
    char const *strings[] = {"dhh", "aci", "cdh", nullptr};
    join_def(strings, 'l');

    return EXIT_SUCCESS;
}

Upvotes: 1

Related Questions