{"id":25593,"date":"2017-10-15T19:29:48","date_gmt":"2017-10-15T13:59:48","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25593"},"modified":"2017-10-15T19:29:48","modified_gmt":"2017-10-15T13:59:48","slug":"c-programming-write-program-print-permutations-given-string-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-write-program-print-permutations-given-string-2\/","title":{"rendered":"C Programming for Write a program to print all permutations of a given string"},"content":{"rendered":"<p>A permutation, also called an \u201carrangement number\u201d or \u201corder,\u201d is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.<span id=\"more-767\"><\/span><br \/>\nSource: Mathword(<a href=\"http:\/\/mathworld.wolfram.com\/Permutation.html\" target=\"_blank\" rel=\"noopener\">http:\/\/mathworld.wolfram.com\/Permutation.html<\/a>)<\/p>\n<p>Below are the permutations of string ABC.<br \/>\nABC ACB BAC BCA CBA CAB<\/p>\n<p>Here is a solution that is used as a basis in backtracking.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25614\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1.png\" alt=\"c\" width=\"1288\" height=\"524\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1.png 1288w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1-300x122.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1-768x312.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1-1024x417.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1-990x403.png 990w\" sizes=\"(max-width: 1288px) 100vw, 1288px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C program to print all permutations with duplicates allowed<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/> <br\/>\/* Function to swap values at two pointers *\/<br\/>void swap(char *x, char *y)<br\/>{<br\/>    char temp;<br\/>    temp = *x;<br\/>    *x = *y;<br\/>    *y = temp;<br\/>}<br\/> <br\/>\/* Function to print permutations of string<br\/>   This function takes three parameters:<br\/>   1. String<br\/>   2. Starting index of the string<br\/>   3. Ending index of the string. *\/<br\/>void permute(char *a, int l, int r)<br\/>{<br\/>   int i;<br\/>   if (l == r)<br\/>     printf(&quot;%s\\n&quot;, a);<br\/>   else<br\/>   {<br\/>       for (i = l; i &lt;= r; i++)<br\/>       {<br\/>          swap((a+l), (a+i));<br\/>          permute(a, l+1, r);<br\/>          swap((a+l), (a+i)); \/\/backtrack<br\/>       }<br\/>   }<br\/>}<br\/> <br\/>\/* Driver program to test above functions *\/<br\/>int main()<br\/>{<br\/>    char str[] = &quot;ABC&quot;;<br\/>    int n = strlen(str);<br\/>    permute(str, 0, n-1);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>ABC\r\nACB\r\nBAC\r\nBCA\r\nCBA\r\nCAB<\/pre>\n<p><strong><br \/>\nAlgorithm Paradigm: <\/strong>Backtracking<br \/>\n<strong>Time Complexity: <\/strong>O(n*n!) Note that there are n! permutations and it requires O(n) time to print a a permutation.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C Programming for Write a program to print all permutations of a given string &#8211; Mathematical Algorithms &#8211; A permutation, also called an \u201carrangement number\u201d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[70323,70348,70351,70331,70326,70340,70338,74276,70366,74267,70328,74261,73693,70384,73690,74275,74262,70329,70347,74271,73711,74263,74259],"class_list":["post-25593","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-permutation-and-combination","tag-permutation-and-combination-calculator","tag-permutation-and-combination-examples","tag-permutation-and-combination-formula","tag-permutation-and-combination-pdf","tag-permutation-and-combination-problems","tag-permutation-combination","tag-permutation-combination-examples","tag-permutation-combination-formula","tag-permutation-examples-math","tag-permutation-formula","tag-permutation-formula-calculator","tag-permutation-of-string","tag-permutation-program-in-c","tag-permutation-questions","tag-permutation-with-repetition","tag-permutation-word-problems","tag-permutations-and-combinations","tag-permutations-and-combinations-formula","tag-permutations-and-combinations-pdf","tag-printall","tag-software-engineer-interview-questions-and-answers","tag-solving-for-r-in-permutations"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25593"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25593\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}