Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Write a C program to print all permutations of a given string.

user-image
Question added by Raouf Rufi , Graduate Software Engineer , tesco
Date Posted: 2013/12/22
aliabdallahsaeed aubaseed
by aliabdallahsaeed aubaseed , مندوب مشتريات , مجموعة شركات الراجحي المحدودة / شركة اسمنت عطبرة المحدودة

 permutation, also called an “arrangement number” or “order,” 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.Source: Mathword(http://mathworld.wolfram.com/Permutation.html)

Below are the permutations of string ABC.ABC, ACB, BAC, BCA, CAB, CBA

Here is a solution using backtracking.

NewPermutation

# include <stdio.h>   /* Function to swap values at two pointers */ void swap (char *x, char *y) { char temp; temp = *x; *x = *y; *y = temp; }   /* Function to print permutations of string This function takes three parameters: 1. String 2. Starting index of the string 3. Ending index of the string. */ void permute(char *a, int i, int n) { int j; if (i == n) printf("%s\\n", a); else { for (j = i; j <= n; j++) { swap((a+i), (a+j)); permute(a, i+1, n); swap((a+i), (a+j)); //backtrack } } }   /* Driver program to test above functions */ int main() { char a[] = "ABC"; permute(a,0,2); getchar(); return

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.