{"id":1800,"date":"2016-03-12T00:01:06","date_gmt":"2016-03-12T08:01:06","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1800"},"modified":"2016-03-19T08:14:59","modified_gmt":"2016-03-19T15:14:59","slug":"recursive-permutations","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1800","title":{"rendered":"Recursive Permutations"},"content":{"rendered":"<p>Recursion is a type of programming loop, one that&#8217;s deliberately designed to drive some programmers nuts. It&#8217;s also a great way to replace nested loops, such as those used in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1794\">last week&#8217;s Lesson<\/a>.<br \/>\n<!--more--><br \/>\nBack in 2014, I write a <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1060\">series of Lessons<\/a> on recursion, which can help you to understand the concept if it&#8217;s bone-dry. In a nutshell, a recursive function calls itself. The function can keep calling itself to repeat whatever action is necessary. Eventually an end condition unwinds all the calls, returning back to sanity.<\/p>\n<p>For nested loops, recursion presents a solution that allows you to perform calculations when you don&#8217;t know the exact number of nested loops required.<\/p>\n<p>As an example, suppose your code must crack a password <em>n<\/em> characters long. If you knew that <em>n<\/em> couldn&#8217;t be greater than 24, then you write 24 nested <em>for<\/em> loops and craft some way that the generated string could match the target string. That solution works, but it&#8217;s a lot of overhead and repetition. Any experienced programmer recognizes that repetition in the code begs for a more elegant solution.<\/p>\n<p>The following code sets up recursion in the <em>compare()<\/em> function. The example omits the recursive function, which I&#8217;ll cover in next week&#8217;s Lesson.<\/p>\n<pre class=\"screen\">#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nint compare(int pos,int len,char *s1,char *s2);\r\n\r\nint main()\r\n{\r\n    char password[24] = \"\\0\";\r\n    char brute[24] = \"\\0\";\r\n    int length;\r\n\r\n    <span class=\"comments\">\/* fetch the password *\/<\/span>\r\n    printf(\"Type a password (23 chars max): \");\r\n    scanf(\"%24s\",password);\r\n    length = strlen(password);\r\n\r\n    <span class=\"comments\">\/* start the recursive comparison *\/<\/span>\r\n    compare(0,length,brute,password);\r\n\r\n    return(0);\r\n}\r\n\r\nint compare(int pos,int len,char *s1,char *s2)\r\n{\r\n    return(FALSE);\r\n}<\/pre>\n<p>The code initializes two buffers at Lines 11 and 12. The user is prompted to type a password at Line 16. That&#8217;s the text the program&#8217;s code attempts to match. The text&#8217;s length is calculated at Line 18 and stored in the <code>length<\/code> variable.<\/p>\n<p>The recursive function <em>compare()<\/em> is called at Line 21. Its arguments are <code>pos<\/code>, which is a position indicator for the guessing string <code>brute<\/code>; <code>len<\/code>, the length of the target string, <code>password<\/code>; and the two buffers.<\/p>\n<p>The stub for the <em>compare()<\/em> function simply returns the FALSE constant. <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1809\">Next week<\/a>, I&#8217;ll fill in the function, which serves two purposes: First to build the guessing string (<code>brute<\/code>) and to compare it with the target string, <code>password<\/code>.<\/p>\n<p>If you want a preview, you can <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/02\/0319.c\" rel=\"\">click here<\/a> to see the code from next week&#8217;s Lesson. You&#8217;ll have to wait until next week, however, to read my explanation of how it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apply the madness of recursion to explore text permutations. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1800\">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-1800","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\/1800","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=1800"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1800\/revisions"}],"predecessor-version":[{"id":1833,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1800\/revisions\/1833"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}