{"id":4995,"date":"2021-10-08T00:01:00","date_gmt":"2021-10-08T07:01:00","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4995"},"modified":"2021-10-02T09:37:16","modified_gmt":"2021-10-02T16:37:16","slug":"alphabedecimal-revisited-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4995","title":{"rendered":"Alphabedecimal Revisited &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4989\">this month&#8217;s Exercise<\/a> is to count in &#8220;alphabedecimal,&#8221; generating output from AAAA through ZZZZ, without using nested loops or a complex <em>if<\/em> structure. Your solution must use a recursive function that handles flipping the digits, er, characters.<br \/>\n<!--more--><br \/>\nThis new approach to the old alphabedecimal puzzle stemmed from a project where I try to brute-force guess passwords. It&#8217;s an interesting puzzle, one that appears in an upcoming book. The attempt made me aware of a cleaner, better way to handle flipping digits in alphabetic output.<\/p>\n<p>For my solution, I created the recursive function, <em>flip()<\/em>:<\/p>\n<p><code>int flip(char *c,int pos);<\/code><\/p>\n<p>The function requires two arguments: a string to manipulate (<code>char *c<\/code>) and a character position to flip(<code>int pos<\/code>). The position argument is the key to eliminating the cascading <em>if<\/em> statements from the earlier solution.<\/p>\n<p>The <em>flip<\/em> function calls itself when a character at the given position flips from Z to A. The position indicator is incremented and the next character in the string is incremented by calling the <em>flip()<\/em> function again.<\/p>\n<p>The <em>flip()<\/em> function returns values 1 (TRUE) or 0 (FALSE) depending on whether the final character has flipped. When the final character is finally flipped, the function returns 0 (FALSE). This value falls through all the recursive calls to end the process.<\/p>\n<p>Here is the full code for my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_10-Exercise.c\" rel=\"noopener\" target=\"_blank\">2021_10-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint flip(char *c,int pos)\r\n{\r\n    (*(c+pos))++;                   <span class=\"comments\">\/* increment the letter *\/<\/span>\r\n    if( *(c+pos) &gt; 'Z' )            <span class=\"comments\">\/* check for overflow *\/<\/span>\r\n    {\r\n        *(c+pos) = 'A';             <span class=\"comments\">\/* reset back to a *\/<\/span>\r\n        if( pos!=0 )                <span class=\"comments\">\/* watch for overflow *\/<\/span>\r\n        {\r\n            if( !flip(c,pos-1) )    <span class=\"comments\">\/* flip the next column's digit *\/<\/span>\r\n                return(0);          <span class=\"comments\">\/* echo down the FALSE return *\/<\/span>\r\n        }\r\n        else\r\n            return(0);              <span class=\"comments\">\/* last column, FALSE return *\/<\/span>\r\n    }\r\n    return(1);                      <span class=\"comments\">\/* keep looping *\/<\/span>\r\n}\r\n\r\nint main()\r\n{\r\n    const unsigned length = 4;\r\n    char digits[length] = \"AAAA\";\r\n\r\n    do\r\n    {\r\n        puts(digits);\r\n    }\r\n    while( flip(digits,length-1) );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 5, The <em>flip()<\/em> function increments the character at position <code>pos<\/code>. If this character is greater than <code>'Z'<\/code>, it&#8217;s reset back to <code>'A'<\/code>. The <code>pos<\/code> variable is compared with zero, which indicates the last character in the string. If <code>pos<\/code> isn&#8217;t zero, the <em>flip()<\/em> function is recursively called, incrementing the next digit in the string.<\/p>\n<p>Because the <em>flip()<\/em> function does all the work, the <em>main()<\/em> function requires only a <em>do-while<\/em> loop to process the string. A <em>puts()<\/em> statement outputs the characters in <code>digits[]<\/code> as they turn from AAAA through ZZZZ.<\/p>\n<p>I hope you devised a similar solution, using a recursive function to process characters in a string in a sequential manner. A recursive function eliminates not only nested <em>for<\/em> loops but a complex, cascading <em>if<\/em> decision tree.<\/p>\n<p>By the way, I compared the timing for this solution with my <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3355\">original Exercise solution<\/a>. Both run at about the same speed. I saw no advantage to making the function recursive versus the cascading <em>if<\/em> structure thing. Yet, I still prefer this solution to the problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to count in &#8220;alphabedecimal,&#8221; generating output from AAAA through ZZZZ, without using nested loops or a complex if structure. Your solution must use a recursive function that handles flipping the digits, er, characters.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4995","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4995","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=4995"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4995\/revisions"}],"predecessor-version":[{"id":5010,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4995\/revisions\/5010"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}