{"id":1809,"date":"2016-03-19T00:01:04","date_gmt":"2016-03-19T07:01:04","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1809"},"modified":"2016-03-12T09:53:41","modified_gmt":"2016-03-12T17:53:41","slug":"a-recursive-permutation-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1809","title":{"rendered":"A Recursive Permutation Function"},"content":{"rendered":"<p>Of course recursion is scary! At no other time in your programming career is your code more susceptible to stack overflows, which appear on the screen as ugly system errors when the code runs. I find that exciting.<br \/>\n<!--more--><br \/>\nRecursion is a solution to the permutation problem when the length of text characters isn&#8217;t fixed. Instead, the length is a variable; the string is <em>n<\/em> characters long. The permutations range from <code>aaa...a<\/code> through <code>zzz...z<\/code>. So the string&#8217;s length is what the recursive function must deal with, and that length is what determines when the function starts to unwind.<\/p>\n<p>Inside the recursive function you need a loop to generate characters from <code>a<\/code> to <code>z<\/code>. The recursive nature of the function slides that loop&#8217;s position to each element in the array, from 0 through <em>n<\/em>.<\/p>\n<p>Sounds weird? Yeah, it is.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/02\/0319.c\"rel=\"\">Click here<\/a> to view the full code for the recursive password-cracking program. The recursive function is named <em>compare()<\/em>, which is listed here:<\/p>\n<pre class=\"screen\">\r\nint compare(int pos,int len,char *s1,char *s2)\r\n{\r\n    char c;\r\n\r\n    if( pos == len)\r\n    {\r\n        if( strcmp(s1,s2)==0 )\r\n        {\r\n            printf(\"%s -&gt; %s\\n\",s1,s2);\r\n            return(TRUE);\r\n        }\r\n        else\r\n            return(FALSE);\r\n    }\r\n    else\r\n    {\r\n        for( c='a'; c&lt;='z' ; c++ )\r\n        {\r\n            s1[pos] = c;\r\n            pos++;\r\n            if( compare(pos,len,s1,s2) )\r\n                return(TRUE);\r\n            pos--;\r\n        }\r\n    }\r\n    return(FALSE);\r\n}<\/pre>\n<p>The function contains two parts. The first is at Line 30:<\/p>\n<pre><code>    if( pos == len)<\/code><\/pre>\n<p><code>pos<\/code> is the position indicator, the key that eventually unwinds the recursion. It represents the character position in string <code>s1<\/code>. When its length is equal to the length of string <code>s2<\/code>, a comparison is made. The <em>compare()<\/em> function then returns to itself with a value of either TRUE or FALSE, depending on the results of the <em>strcmp()<\/em> function.<\/p>\n<blockquote><p>It may seem like <code>pos<\/code> is greater than <code>len<\/code>, which is confusing. Remember that <code>pos<\/code> references an array element. The last character in a 4-character string sits at element 3 in the array. When <code>pos<\/code> is equal to <code>len<\/code>, both strings are the same size and can be compared.<\/p><\/blockquote>\n<p>The second part is the condition when the two strings aren&#8217;t of equal length, which starts with the <em>else<\/em> statement at Line 40. In this chunk of code, the <em>compare()<\/em> function builds string <code>s1<\/code>.<\/p>\n<p>The <em>for<\/em> loop at Line 42 marches from <code>a<\/code> to <code>z<\/code>. The character is put into element <code>pos<\/code> in <em>char<\/em> array <code>s1<\/code>. The position <code>pos<\/code> is incremented to the next element in the array. <em>compare()<\/em> is called recursively at Line 46 to process that next element.<\/p>\n<p>As long as <code>pos &lt; len<\/code>, the <em>else<\/em> portion of the <em>compare()<\/em> function continues to build string <code>s1<\/code>. Eventually, the string <code>aaa...a<\/code> is created, which is equal to the length of target string <code>s2<\/code>. At that point, <code>pos == len<\/code> and the strings are compared. If the strings match, TRUE is returned and the recursion unwinds. Otherwise, FALSE is returned. At that point, Line 48, the <code>pos<\/code> indicator is decremented, and the next letter in the loop is processed, <code>aaa...b<\/code>.<\/p>\n<p>The <em>for<\/em> loop continues to change each character in the <code>s1<\/code> array, just as the nested loops did in an <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1794\">earlier post<\/a>. As long as the <em>compare()<\/em> function returns to itself with a FALSE value, the loop keeps permuting string <code>s1<\/code>. It spins through all permutations up until <code>zzz...z<\/code> and finally returns FALSE at Line 51 when the two strings cannot match.<\/p>\n<p>This recursive technique isn&#8217;t any faster than a solution using nested <em>for<\/em> loops; the longer the string you type, the longer it takes Mr. Computer to crunch the results. A long string composed of a&#8217;s and b&#8217;s will process more quickly. If you type a long string with lots of z&#8217;s in it, expect to wait a while.<\/p>\n<p>The only advantage of using recursion is that the same function works regardless of the length of the target string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The key to recursion is to ensure that the function eventually unwinds itself. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1809\">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-1809","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\/1809","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=1809"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1809\/revisions"}],"predecessor-version":[{"id":1827,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1809\/revisions\/1827"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}