Alex Gordon
Alex Gordon

Reputation: 60871

Add 2 chars without using strncpy?

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

Upvotes: 0

Views: 3486

Answers (5)

knight666
knight666

Reputation: 1619

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?

Upvotes: 1

Thomas Matthews
Thomas Matthews

Reputation: 57749

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.

Upvotes: 1

Emile Cormier
Emile Cormier

Reputation: 29229

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

Upvotes: 4

Frank Krueger
Frank Krueger

Reputation: 71053

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.

Upvotes: 3

Brian
Brian

Reputation: 25834

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

Upvotes: 3

Related Questions