To Copy or to Duplicate a String

The C library function to copy a string is strcpy(), which (I’m guessing) stands for string copy.

Here’s the format:

char * strcpy(char * dst, const char * src);

The dst is the destination, src is the source or original string. The destination’s allocated space must be of the same size (or larger) than the source. The value returned is a pointer to the dst string.

The strcpy() function duplicates string scr to the location specified by dst one character at a time until and including the null character, \0. Here’s some sample code:

#include <stdio.h>
#include <string.h>

int main()
{
    char original[12] = "Ooga Booga!";
    char duplicate[12];
    int o_len,d_len;

    strcpy(duplicate,original);
    o_len = strlen(original);
    d_len = strlen(duplicate);

    printf("Original String: '%s' (%d)\n",
            original,o_len);
    printf("Duplicate string: '%s' (%d)\n",
            duplicate,d_len);

    return(0);
}

The strcpy() function requires the string.h header file, as shown in Line 2. At Line 7, space is allocated for the duplicate string. I chose to use an empty char array. If you use a pointer, then you must allocate space for the string copy; the space must be equal to the size of the original string.

Line 10 copies the string. The rest of the code fetches the string length and displays the original, the copy, and their lengths. Everything should match up in the output:

Original String: 'Ooga Booga!' (11)
Duplicate string: 'Ooga Booga!' (11)

A second string copying function is available in some C language libraries, strdup(). It works similar to the strcpy() function, but you need only supply a char pointer as the first argument. The function itself allocates space for the duplicate string. This function is better than strcpy() in that it creates string copies in cases when you may not know the exact size of the original string.

Here’s sample code:

#include <stdio.h>
#include <string.h>

int main()
{
    char original[12] = "Ooga Booga!";
    char *duplicate;
    int o_len,d_len;

    duplicate = strdup(original);
    o_len = strlen(original);
    d_len = strlen(duplicate);

    printf("Original String: '%s' (%d)\n",
            original,o_len);
    printf("Duplicate string: '%s' (%d)\n",
            duplicate,d_len);

    return(0);
}

The duplicate string’s destination is declared as a char pointer at Line 7. The strdup() function appears at Line 10. It’s format uses the original string as the only argument. The duplicate is returned, or the NULL pointer in case of failure.

The output for the above code is the same as for the first example.

Both strcpy() and strdup() have sisters that copy only n characters into the new string. These are the strncpy() and strndup() functions. Here are the formats:

char * strncpy(char * dst, const char * src, size_t len));
char * strndup(const char *s1, size_t n);

In the strncpy() function, the value returned is a pointer to the null character (\0) at the end of the dst string. For strndup(), the duplicated string is returned.

And remember: The strdup() and strndup() functions don’t appear in every C language library. If not, you can craft them on your own. I’ll provide examples in next week’s Lesson.

Leave a Reply