tags:

views:

196

answers:

5

How would I manually concatenate two char arrays without using the strncpy function?

Can I just say char1 + char2?

Or would I have to write a for loop to get individual elements and add them like this:

addchar[0] = char1[0];
addchar[1] = char1[1];
etc
etc
addchar[n] = char2[0];
addchar[n+1] = char2[1];
etc
etc

To clarify, if char1 = "happy" char2 = "birthday"

I want addchar to = happybirthday

+4  A: 

For a C-only solution use strncat:

char destination[80] = "";
char string1[] = "Hello";
char string2[] = " World!";


/* Copy string1 to destination */
strncat(destination, string1, sizeof(destination));

/* Append string2 to destination */
strncat(destination, string2, sizeof(destination) - sizeof(string1));

Note that the strn* family of string functions are safer than the ones without n, because they avoid the possibility of buffer overruns.

For a C++ solution, simply use std::string and operator+ or operator+=:

std::string destination("Hello ");
destination += "World";
destination += '!';
Emile Cormier
without using strncpy please
I__
I thought every_answer_gets_a_point ?
Frank Krueger
Edited to avoid using strncpy. Do you want to avoid string functions altogether? If so, please indicate in your question.
Emile Cormier
+3  A: 

If you're using c++ just use an std::string. With std::strings, the + operator is supported, so you can do string1+string2.

Brian
+2  A: 

If you consider two trivial loops to be "manual", then yes, without using the standard library this is the only way.

char *append(const char *a, const char *b) {
    int i = 0;
    size_t na = strlen(a);
    size_t nb = strlen(b);
    char *r = (char*)calloc(na + nb + 1, 1);
    for (i = 0; i < na; i++) {
        r[i] = a[i];
    }
    for (i = 0; i < nb; i++) {
        r[na + i] = b[i];
    }
    return r;
}

Remember to call free.

Frank Krueger
To the OP: This may be how `strcat` is coded.
Thomas Matthews
It would be better (and more consistent with the standard library) if the caller supplied an already-allocated destination buffer (along with its size). It is good practice that the one responsible for freeing memory is the one responsible for allocating it.
Emile Cormier
"Foolish consistency is the hobgoblin of small minds." Just kidding, just kidding! But honestly, the design of the standard library seems to have favored CPU productivity over human. I do not like its interfaces and choose not to propagate them. (Except, `atoi`, that function is OK.)
Frank Krueger
C and its standard library were developed in the 70's/80's when resources were constrained. C's current niche is still in resource-constrained environments. If human productivity is of utmost concern, then there are other languages that are more appropriate than C.
Emile Cormier
+1  A: 

Without using library functions, here is the procedure:
1. Point to the first character in string1.
2. While the current character at the pointer is not null, increment the pointer.
3. Create a "source" pointer pointing to string2.
4. While the character at the "source" location is not null:
4.1. Copy the character from the "source" location to the location pointed to by the String1 pointer.
4.2. Increment both pointers.

Unless this is homework, use C++ std::string for your text.
If you must use C style strings, use the library functions.
Library functions are optimized and validated, reducing your development time.

Thomas Matthews
+1  A: 

Alright, you want something like this:

char1 + char2

First, let's see the insane solution:

C:

char* StringAdd(char* a_Left, char* a_Right)
{
    unsigned int length_left = strlen(a_Left);
    unsigned int length_right = strlen(a_Right);
    unsigned int length = length_left + length_right;

    char* result = (char*)malloc(length);

    // clear the string
    memset(result, 0, length);

    // copy the left part to the final string
    memcpy(result, a_Left, length_left);

    // append the right part the to the final string
    memcpy(&result[length_left], a_Right, length_right);

    // make sure the string actually ends
    result[length] = 0;

    return result;
}

C++:

char* StringAdd(char* a_Left, char* a_Right)
{
    unsigned int length_left = strlen(a_Left);
    unsigned int length_right = strlen(a_Right);
    unsigned int length = length_left + length_right;

    char* result = new char[length];

    // clear the string
    memset(result, 0, length);

    // copy the left part to the final string
    memcpy(result, a_Left, length_left);

    // append the right part the to the final string
    memcpy(&result[length_left], a_Right, length_right);

    // make sure the string actually ends
    result[length] = 0;

    return result;
}

Now, let's see the sane solution:

char* StringAdd(char* a_Left, char* a_Right)
{
    unsigned int length = strlen(a_Left) + strlen(a_Right);

    char* result = new char[length];
    strcpy(result, a_Left);
    strcat(result, a_Right);

    return result;
}

So, was this homework? I don't really care.

If it was, ask yourself: what did you learn?

knight666
You have a buffer overflow bug.
Frank Krueger
I could say I left it as an exercise to the reader, but I'd be lying.
knight666