{"id":3626,"date":"2019-06-08T00:01:53","date_gmt":"2019-06-08T07:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3626"},"modified":"2019-06-01T10:04:59","modified_gmt":"2019-06-01T17:04:59","slug":"old-habits-die-hard","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3626","title":{"rendered":"Old Habits Die Hard"},"content":{"rendered":"<p>Like doing anything for a long period of time, I find myself often using the same approach to solving a programming puzzle. It&#8217;s habit. It&#8217;s tradition. And it often hinders my insight into the language.<br \/>\n<!--more--><br \/>\nAs an example, when I write code that works through a string, I always use a <em>while<\/em> loop. Always. I suppose that&#8217;s the way I learned how to manipulate a string one character at a time. To wit:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"Of all the gin joints in the world...\\n\";\r\n    int x;\r\n\r\n    x = 0;\r\n    while( string[x] )\r\n    {\r\n        putchar( string[x] );\r\n        x++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop keeps spinning until the value of <code>string[x]<\/code> is false, which means it returns the null character in this example. The three parts of the <em>while<\/em> loop are split between three lines:<\/p>\n<ul>\n<li> Initialization of variable <code>x<\/code> at Line 8<\/li>\n<li>The exit condition specified at Line 9.<\/li>\n<li>The loop stepping value at Line 12.<\/li>\n<\/ul>\n<p>This code represents my traditional approach, but it&#8217;s not the only way to process a string one character at a time.<\/p>\n<p>In the code below, I use a <em>for<\/em> loop to process a string. I&#8217;ve never used a <em>for<\/em> loop in this manner; normally I use a <em>for<\/em> loop as a counter, but yet it can work through characters just like a <em>while<\/em> loop:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"Of all the gin joints in the world...\\n\";\r\n    int x;\r\n\r\n    for( x=0; string[x]; x++)\r\n    {\r\n        putchar( string[x] );\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Unlike the preceding example, in the code above all the loop elements appear in one place, within the <em>for<\/em> loop&#8217;s parentheses at Line 8. In fact, the loop can be tightened further:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"Of all the gin joints in the world...\\n\";\r\n    int x;\r\n\r\n    for( x=0; putchar(string[x]) ; x++)\r\n        ;\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>putchar()<\/em> function returns the value of the character output, which means the null character can still stop the loop. The sole semicolon on Line 9 signals to the compiler that I&#8217;m aware the loop has no statements and did not unintentionally create an endless loop.<\/p>\n<p>Now that I recognize I&#8217;m stuck in a rut with my approach to processing strings, I&#8217;m curious see what other habits I have that can be broken. I must plow through my code to see whether my approach to solving a problem is done from tradition. Certainly other methods must be available. If I desire to boost my programming kung fu, it helps to be aware of such peccadilloes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not all programming puzzles must be solved the same way. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3626\">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-3626","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\/3626","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=3626"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3626\/revisions"}],"predecessor-version":[{"id":3638,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3626\/revisions\/3638"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}