{"id":5967,"date":"2023-08-01T00:01:08","date_gmt":"2023-08-01T07:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5967"},"modified":"2023-08-19T09:09:50","modified_gmt":"2023-08-19T16:09:50","slug":"from-camelcase-to-snake_case","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5967","title":{"rendered":"From camelCase to snake_case"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>The Great Coding Wars host many battles. For example, VIM versus Emacs (add in Nano for extra military action). Also the fight between spaces and tabs for indenting. Add to these conflicts a minor skirmish: whether to use camelCase or snake_case naming conventions.<br \/>\n<!--more--><br \/>\nI&#8217;m not a stickler for either method, and I often tend to mix both. Still, names for variables, functions, methods, and the lot are classified into two major naming schemes: camelCase and snake_case.<\/p>\n<p>The camelCase convention uses mid-text capital letters to easily identify words. Because you can&#8217;t use spaces in a long, descriptive name, the capitals assist with readability. For example, <em>purgeOldData()<\/em> for a function that purges old data.<\/p>\n<p>The snake_case convention also assists in identifying words in a name, with the words separated by underscores. For example, the variable <code>terminal_status<\/code> represents the terminal connection&#8217;s current status.<\/p>\n<p>I use both methods in my code, and often I commit the sin of mixing them in a perverted manner: <em>fix_Input()<\/em> Regardless, I&#8217;m certain programmers and developers have their preferences and woe be to the humble coder who disagrees.<\/p>\n<p>The challenge for this month&#8217;s Exercise is to write code that identifies the naming convention, camelCase or snake_case, and converts the name to the other convention. To avoid any weird names, I&#8217;ve provided a consistent list for you in sample code here:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_08_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_08_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.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    int x;\r\n\r\n    for(x=0; x&lt;count; x++ )\r\n    {\r\n        printf(\"%s\\n\",variable[x]);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Your task is to modify this code so that it identifies naming the convention, then outputs the same name but in the other convention: snake_case to camelCase and camelCase to snake_case.<\/p>\n<p>Here is a sample run of my solution:<\/p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readInputMeter&nbsp;-&gt;&nbsp;read_input_meter<br \/>\ncyclical_redundancy_check&nbsp;-&gt;&nbsp;cyclicalRedundancyCheck<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bumpyRide&nbsp;-&gt;&nbsp;bumpy_ride<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search_for_node&nbsp;-&gt;&nbsp;searchForNode<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string_convert&nbsp;-&gt;&nbsp;stringConvert<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;divideByZeroError&nbsp;-&gt;&nbsp;divide_by_zero_error<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;giveUpAndExplode&nbsp;-&gt;&nbsp;give_up_and_explode<\/code><\/p>\n<p>Please try this Exercise on your own before you examine <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5980\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write code that identifies and converts between the two common naming conventions. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5967\">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":[3],"tags":[],"class_list":["post-5967","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5967","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=5967"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5967\/revisions"}],"predecessor-version":[{"id":6010,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5967\/revisions\/6010"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}