{"id":1690,"date":"2015-12-26T00:01:22","date_gmt":"2015-12-26T08:01:22","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1690"},"modified":"2016-01-02T08:04:49","modified_gmt":"2016-01-02T16:04:49","slug":"to-copy-or-to-duplicate-a-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1690","title":{"rendered":"To Copy or to Duplicate a String"},"content":{"rendered":"<p>The C library function to copy a string is <em>strcpy()<\/em>, which (I&#8217;m guessing) stands for <em>string copy<\/em>.<br \/>\n<!--more--><br \/>\nHere&#8217;s the format:<\/p>\n<pre><code>char * strcpy(char * dst, const char * src);<\/code><\/pre>\n<p>The <code>dst<\/code> is the destination, <code>src<\/code> is the source or original string. The destination&#8217;s allocated space must be of the same size (or larger) than the source. The value returned is a pointer to the <code>dst<\/code> string.<\/p>\n<p>The <em>strcpy()<\/em> function duplicates string <code>scr<\/code> to the location specified by <code>dst<\/code> one character at a time until and including the null character, <code>\\0<\/code>. Here&#8217;s some sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char original[12] = \"Ooga Booga!\";\r\n    char duplicate[12];\r\n    int o_len,d_len;\r\n\r\n    strcpy(duplicate,original);\r\n    o_len = strlen(original);\r\n    d_len = strlen(duplicate);\r\n\r\n    printf(\"Original String: '%s' (%d)\\n\",\r\n            original,o_len);\r\n    printf(\"Duplicate string: '%s' (%d)\\n\",\r\n            duplicate,d_len);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>strcpy()<\/em> function requires the <code>string.h<\/code> header file, as shown in Line 2. At Line 7, space is allocated for the duplicate string. I chose to use an empty <em>char<\/em> 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.<\/p>\n<p>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:<\/p>\n<pre><code>Original String: 'Ooga Booga!' (11)\r\nDuplicate string: 'Ooga Booga!' (11)<\/code><\/pre>\n<p>A second string copying function is available in some C language libraries, <em>strdup()<\/em>. It works similar to the <em>strcpy()<\/em> function, but you need only supply a <em>char<\/em> pointer as the first argument. The function itself allocates space for the duplicate string. This function is better than <em>strcpy()<\/em> in that it creates string copies in cases when you may not know the exact size of the original string.<\/p>\n<p>Here&#8217;s sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char original[12] = \"Ooga Booga!\";\r\n    char *duplicate;\r\n    int o_len,d_len;\r\n\r\n    duplicate = strdup(original);\r\n    o_len = strlen(original);\r\n    d_len = strlen(duplicate);\r\n\r\n    printf(\"Original String: '%s' (%d)\\n\",\r\n            original,o_len);\r\n    printf(\"Duplicate string: '%s' (%d)\\n\",\r\n            duplicate,d_len);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The duplicate string&#8217;s destination is declared as a <em>char<\/em> pointer at Line 7. The <em>strdup()<\/em> function appears at Line 10. It&#8217;s format uses the original string as the only argument. The duplicate is returned, or the <code>NULL<\/code> pointer in case of failure.<\/p>\n<p>The output for the above code is the same as for the first example.<\/p>\n<p>Both <em>strcpy()<\/em> and <em>strdup()<\/em> have sisters that copy only <em>n<\/em> characters into the new string. These are the <em>strncpy()<\/em> and <em>strndup()<\/em> functions. Here are the formats:<\/p>\n<pre><code>char * strncpy(char * dst, const char * src, size_t len));\r\nchar * strndup(const char *s1, size_t n);<\/code><\/pre>\n<p>In the <em>strncpy()<\/em> function, the value returned is a pointer to the null character (<code>\\0<\/code>) at the end of the <code>dst<\/code> string. For <em>strndup()<\/em>, the duplicated string is returned.<\/p>\n<p>And remember: The <em>strdup()<\/em> and <em>strndup()<\/em> functions don&#8217;t appear in every C language library. If not, you can craft them on your own. I&#8217;ll provide examples in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1699\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C Library offers two string replication functions. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1690\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1690","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1690"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1690\/revisions"}],"predecessor-version":[{"id":1716,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1690\/revisions\/1716"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}