C Program to Find String Permutations
This is the C Program to find string permutations there are n! permutations for a word of size n
In mathematics, the notion of permutation relates to the act of rearranging, or permuting, all the members of a set into some sequence or order (unlike combinations, which are selections of some members of the set where order is disregarded). For example, written as tuples, there are six permutations of the set {1,2,3}, namely: (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1).
#include<stdio.h>
#include<string.h>
int len,ctr=0;
main(){
char word[100];
printf("Enter a Wordn");
scanf("%s",word);
len=strlen(word);
printf("There are %d Permutationsn",fact(len));
perm(word);
}
int fact(int n)
{
if (n==1)return 1;
else return n*fact(n-1);
}
void perm(char *word)
{
int i;
char swap;
for(i=0;i<len-1;i++)
{
swap=word[i];
word[i]=word[i+1];
word[i+1]=swap;
ctr++;
printf("%d.t%sn",ctr,word);
}
if(ctr==fact(len)) return 1;
perm(word);
}
Recent Comments