{"id":5996,"date":"2023-08-19T00:01:05","date_gmt":"2023-08-19T07:01:05","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5996"},"modified":"2023-08-12T09:39:35","modified_gmt":"2023-08-12T16:39:35","slug":"the-camelcase-to-snake_case-conversion-with-proper-memory-allocation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5996","title":{"rendered":"The camelCase to snake_case Conversion with Proper Memory Allocation"},"content":{"rendered":"<p>With all the storage available in a modern computer, it&#8217;s easy &mdash; and often perfectly okay &mdash; to be overly generous when allocating memory. Still, the old coder in me has a lingering desire to save every byte possible. So when it comes to crafting a solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5967\">this month&#8217;s Exercise<\/a>, my desire is to be byte stingy.<br \/>\n<!--more--><br \/>\nThe issue for this week&#8217;s Lesson isn&#8217;t to convert between camelCase and snake_case, but rather to perfectly allocate storage for the converted strings.<\/p>\n<p>For the snake_case to camelCase conversion, fewer characters are required. My approach is to count the number of underscores in the original name, then allocate storage based on the original name&#8217;s string size, minus the number of underscores.<\/p>\n<p>For the camelCase to snake_case conversion, more characters are required. The quantity depends on the number of uppercase characters found in the name. For each uppercase character, an underscore is added. To allocate storage, I use the original string&#8217;s size, plus the number of uppercase characters found.<\/p>\n<p>Here is my new solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_08_19-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_08_19-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\nint main()\r\n{\r\n    const int count = 7;\r\n    char *variable[] = {\r\n        \"readInputMeter\",\r\n        \"cyclical_redundancy_check\",\r\n        \"bumpyRide\",\r\n        \"search_for_node\",\r\n        \"string_convert\",\r\n        \"divideByZeroError\",\r\n        \"giveUpAndExplode\"\r\n    };\r\n    char *v[count];\r\n    int x,c,us,caps;\r\n    char *n;\r\n\r\n    for(x=0; x&lt;count; x++ )\r\n    {\r\n        n = variable[x];        <span class=\"comments\">\/* initialize pointer n *\/<\/span>\r\n        c = 0;                    <span class=\"comments\">\/* initialize offset *\/<\/span>\r\n\r\n        <span class=\"comments\">\/* test for the underscore *\/<\/span>\r\n        if( strchr(variable[x],'_') )\r\n        {\r\n            <span class=\"comments\">\/* name is in snake_case *\/<\/span>\r\n            <span class=\"comments\">\/* count the underscores *\/<\/span>\r\n            us = 0;\r\n            while( *n )\r\n            {\r\n                if( *n=='_' )\r\n                    us++;\r\n                n++;\r\n            }\r\n            <span class=\"comments\">\/* re-initialize n *\/<\/span>\r\n            n = variable[x];\r\n            <span class=\"comments\">\/* allocate proper storage *\/<\/span>\r\n            v[x] = malloc( strlen(variable[x]) - us + 1 );\r\n            if( v[x]==NULL )\r\n            {\r\n                fprintf(stderr,\"Memory allocate error\\n\");\r\n                exit(1);\r\n            }\r\n            <span class=\"comments\">\/* build the new string *\/<\/span>\r\n            while( *n )\r\n            {\r\n                if( *n=='_' )\r\n                {\r\n                    n++;\r\n                    *(v[x]+c) = toupper(*n);\r\n                }\r\n                else\r\n                {\r\n                    *(v[x]+c) = *n;\r\n                }\r\n                n++;\r\n                c++;\r\n            }\r\n        }\r\n        else\r\n        {\r\n            <span class=\"comments\">\/* name is in camelCase *\/<\/span>\r\n            <span class=\"comments\">\/* count the capital letters *\/<\/span>\r\n            caps = 0;\r\n            while( *n )\r\n            {\r\n                if( isupper(*n) )\r\n                    caps++;\r\n                n++;\r\n            }\r\n            <span class=\"comments\">\/* re-initialize n *\/<\/span>\r\n            n = variable[x];\r\n            <span class=\"comments\">\/* allocate proper storage *\/<\/span>\r\n            v[x] = malloc( strlen(variable[x]) + caps + 1 );\r\n            if( v[x]==NULL )\r\n            {\r\n                fprintf(stderr,\"Memory allocate error\\n\");\r\n                exit(1);\r\n            }\r\n            <span class=\"comments\">\/* build the new string *\/<\/span>\r\n            while( *n )\r\n            {\r\n                if( isupper(*n) )\r\n                {\r\n                    *(v[x]+c) = '_';\r\n                    c++;\r\n                    *(v[x]+c) = tolower(*n);\r\n                }\r\n                else\r\n                {\r\n                    *(v[x]+c) = *n;\r\n                }\r\n                n++;\r\n                c++;\r\n            }\r\n        }\r\n        <span class=\"comments\">\/* cap the string *\/<\/span>\r\n        *(v[x]+c) = '\\0';\r\n    }\r\n\r\n    <span class=\"comments\">\/* output the result *\/<\/span>\r\n    for(x=0; x&lt;count; x++ )\r\n        printf(\"%25s -&gt; %s\\n\",\r\n                variable[x],\r\n                v[x]\r\n              );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>For the string_case to camelCase conversion, the number of underscores are saved in variable <code>us<\/code>. This variable is used to calculate needed storage for the converted string:<\/p>\n<p><code>v[x] = malloc( strlen(variable[x]) - us + 1 );<\/code><\/p>\n<p>The underscore character count (<code>us<\/code>) is subtracted from the original string&#8217;s length: <code>strlen(variable[x])<\/code> Then one is added to create storage for the new string.<\/p>\n<p>When converting from camelCase to string_case, the number of capital letters found is stored in variable <code>caps<\/code>. This value is added to the original string&#8217;s length to allocate storage for the new string:<\/p>\n<p><code>v[x] = malloc( strlen(variable[x]) + caps + 1 );<\/code><\/p>\n<p>Honestly, in the era of multi-gigabyte computers, quibbling over a handful of bytes here and there is silly. Yet it&#8217;s always good practice to be accurate, especially when dealing with larger memory chunks.<\/p>\n<p>The other aspect of this month&#8217;s Exercise is to properly detect whether a name is true camelCase or snake_case. I decided not to pursue this angle, though I may cover such detection methods in a future Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes, you can allocate too much memory, but you can also do the coding to make the allocation perfect. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5996\">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-5996","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\/5996","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=5996"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5996\/revisions"}],"predecessor-version":[{"id":6003,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5996\/revisions\/6003"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}