Here, For loop(instead of recursion) has been used to print all the permutations of a given string in the lexicographic order.
The code executes correctly even for strings having repeated characters.
For example,
For the string:aab
The permutations are: aab,aba,baa.
Source Code:
****************************************************************
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void swap(char*str,int index1,int index2)/*function to swap two characters of a string str
at index1 and index2.*/
{
char temp;
temp=str[index1];
str[index1]=str[index2];
str[index2]=temp;
}
int compare(const void *a,const void *b)
{
return strcmp((char*)a,(char*)b);
}
long long findFactorial(int n) //to find factorial of a number.
{
long long fact=1,i;
for(i=2;i<=n;i++)
{
fact*=i;
}
return fact;
}
long long findNumberOfPermutations(char str[]) //to find the no of permutations of a given string.
{
long long number,repetition,*flag,i;
flag=(long long *)malloc(sizeof(long long )*128);
for(i=0;i<128;i++)
flag[i]=0;
for(i=0;str[i];i++)
flag[str[i]]++;
number=findFactorial(strlen(str));
for(i=0;i<128;i++)
if(flag[i]>1)
number/=findFactorial(flag[i]);
return number;
}
int findMinimum(char str[],int index1)
{
/*
this function returns the index of minimum element in the array
that is greater that element at index1 but minimum in the array from index1+1 till strlen(str)
*/
int minIndex,i;
int min=2147483647;
for(i=index1+1;i<strlen(str);i++)
{
if(str[i]<min && str[i]>str[index1])
{
min=str[i];
minIndex=i;
}
}
return minIndex;
}
void findNextPermutation(char str[]) //to find the next Permutation of string str.
{
int i,index1,index2;
for(i=strlen(str)-1;i>=0;i--)
if(str[i-1]<str[i])
break;
index1=i-1;
index2=findMinimum(str,index1);
swap(str,index1,index2);
qsort(str+index1+1,strlen(str)-index1-1,sizeof(char),compare);
}
void printPermutations(char str[]) //to print all permutations of a string.
{
int i;
long long numberOfPermutations;
numberOfPermutations=findNumberOfPermutations(str);
qsort((void*)str,strlen(str),sizeof(char),compare);
for(i=0;i<numberOfPermutations;i++)
{
printf("%d -> %s\n",i+1,str);
if(i==numberOfPermutations-1)
break;
findNextPermutation(str);
}
}
int main()
{
char str[10000];
printf("\nEnter the string:");
scanf("%s",str);
printPermutations(str);
return 0;
}
****************************************************************
The code executes correctly even for strings having repeated characters.
For example,
For the string:aab
The permutations are: aab,aba,baa.
Source Code:
****************************************************************
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void swap(char*str,int index1,int index2)/*function to swap two characters of a string str
at index1 and index2.*/
{
char temp;
temp=str[index1];
str[index1]=str[index2];
str[index2]=temp;
}
int compare(const void *a,const void *b)
{
return strcmp((char*)a,(char*)b);
}
long long findFactorial(int n) //to find factorial of a number.
{
long long fact=1,i;
for(i=2;i<=n;i++)
{
fact*=i;
}
return fact;
}
long long findNumberOfPermutations(char str[]) //to find the no of permutations of a given string.
{
long long number,repetition,*flag,i;
flag=(long long *)malloc(sizeof(long long )*128);
for(i=0;i<128;i++)
flag[i]=0;
for(i=0;str[i];i++)
flag[str[i]]++;
number=findFactorial(strlen(str));
for(i=0;i<128;i++)
if(flag[i]>1)
number/=findFactorial(flag[i]);
return number;
}
int findMinimum(char str[],int index1)
{
/*
this function returns the index of minimum element in the array
that is greater that element at index1 but minimum in the array from index1+1 till strlen(str)
*/
int minIndex,i;
int min=2147483647;
for(i=index1+1;i<strlen(str);i++)
{
if(str[i]<min && str[i]>str[index1])
{
min=str[i];
minIndex=i;
}
}
return minIndex;
}
void findNextPermutation(char str[]) //to find the next Permutation of string str.
{
int i,index1,index2;
for(i=strlen(str)-1;i>=0;i--)
if(str[i-1]<str[i])
break;
index1=i-1;
index2=findMinimum(str,index1);
swap(str,index1,index2);
qsort(str+index1+1,strlen(str)-index1-1,sizeof(char),compare);
}
void printPermutations(char str[]) //to print all permutations of a string.
{
int i;
long long numberOfPermutations;
numberOfPermutations=findNumberOfPermutations(str);
qsort((void*)str,strlen(str),sizeof(char),compare);
for(i=0;i<numberOfPermutations;i++)
{
printf("%d -> %s\n",i+1,str);
if(i==numberOfPermutations-1)
break;
findNextPermutation(str);
}
}
int main()
{
char str[10000];
printf("\nEnter the string:");
scanf("%s",str);
printPermutations(str);
return 0;
}
****************************************************************
No comments:
Post a Comment