{"id":5988,"date":"2023-08-12T00:01:28","date_gmt":"2023-08-12T07:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5988"},"modified":"2023-08-19T08:28:16","modified_gmt":"2023-08-19T15:28:16","slug":"a-better-camelcase-to-snake_case-conversion","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5988","title":{"rendered":"A Better camelCase to snake_case Conversion"},"content":{"rendered":"<p>My solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5967\">this month&#8217;s Exercise<\/a> took some terrible assumptions. First, that the strings are merely output and not stored. Second, that the strings are perfectly formed camelCase and snake_case. In this Lesson, I address the first concern.<br \/>\n<!--more--><br \/>\nTo store the strings instead of outputting them directly, memory must be allocated. To determine the size, I need to know the original string size and then calculate the new string size based on the type of conversion. The issue here is whether to be spot-on accurate to just provide enough room for the worst case size of any given string.<\/p>\n<p>It&#8217;s easier not to be spot-on accurate.<\/p>\n<p>For the snake_case to camelCase conversion, the new string is always shorter than the original. Therefore, I can allocate storage based on the original string&#8217;s size.<\/p>\n<p>For the camelCase to snake_case conversion, one character is added for each capital in the camelCase name. Here my cheat is to double the original string size, which more than handles a situation where every other character is capitalized (improbable but possible).<\/p>\n<p>Here is my updated solution for this month&#8217;s Exercise. Error-checking on the <em>malloc()<\/em> function is omitted to keep the source code file short:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_08_12-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_08_12-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;\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\">\/* camelCase will be shorter, so just allocate\r\n               the same storage *\/<\/span>\r\n            v[x] = malloc( strlen(variable[x]) + 1 );\r\n            <span class=\"comments\">\/* error checking goes here *\/<\/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\">\/* allocate storage for worst case *\/<\/span>\r\n            v[x] = malloc( strlen(variable[x]) * 2 + 1 );\r\n            <span class=\"comments\">\/* error checking goes here *\/<\/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>I&#8217;ve added several new variables to help build and store the new strings:<\/p>\n<p><code>*v[]<\/code> is a <em>char<\/em> pointer array to store the new, converted strings.<\/p>\n<p><code>c<\/code> is an <em>int<\/em> variable used to calculate offsets within a string. This variable is initialized to zero at each turn of the <em>for<\/em> loop: <code>c = 0<\/code><\/p>\n<p>For the snake_case to camelCase conversion, storage is allocated based on the size of the original string: <code>v[x] = malloc( strlen(variable[x]) + 1 );<\/code><\/p>\n<p>As opposed to being output (from my original solution), characters are stored instead, using variable <code>c<\/code> as the offset within the freshly-allocated buffer: <code>*(v[x]+c) = tolower(*n);<\/code> and <code>*(v[x]+c) = *n;<\/code><\/p>\n<p>Variable <code>n<\/code> is incremented through the original string. Variable <code>c<\/code> is incremented through the new string.<\/p>\n<p>For the camelCase to snake_case conversion, I allocated memory based on double the size of the original string: <code>v[x] = malloc( strlen(variable[x]) * 2 + 1 );<\/code> This is a bit of overkill, which I&#8217;ll address in next week&#8217;s Lesson.<\/p>\n<p>As with the other conversion, variable <code>n<\/code> plows through the original string while variable <code>c<\/code> helps build the new string in the allocated storage.<\/p>\n<p>After the strings are created, their capped with the null character as termination: <code>*(v[x]+c) = '\\0';<\/code><\/p>\n<p>The output is the same as for the original solution, with the new strings are stored and not output directly. I suppose this approach is better, though more improvement is possible. I cover this step in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5996\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an update to my camelCase\/snake_case solution to store the strings instead of output them directly. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5988\">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-5988","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\/5988","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=5988"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5988\/revisions"}],"predecessor-version":[{"id":6006,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5988\/revisions\/6006"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}