#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
int factorial(int
n)
{
int fact=1;
for(int i=2;i<=n;i++)
fact*=i;
return fact;
}
char str[100];
void swap(int
i,int j)
{
char temp=str[i];
str[i]=str[j];
str[j]=temp;
}
void sort(char
**perm,int rows)
{
for(int i=1;i<rows;i++)
{
char *temp;
strcpy(temp,perm[i]);
for(int j=i-1;j>=0 &&
strcmpi(temp,perm[j])<0;j--)
strcpy(perm[j+1],perm[j]);
strcpy(perm[j+1],temp);
}
}
void main()
{
clrscr();
int len,fact;
cout<<"Enter the string:";
gets(str);
len=strlen(str);
fact=factorial(len);
char **permutations = new char*[fact];
for(int i=0;i<fact;i++)
{
int j=i%(len-1);
swap(j,j+1);
permutations[i]=new char[len];
strcpy(permutations[i],str);
}
for(i=0;i<fact;i++)
cout<<"\n"<<i+1<<"."<<permutations[i];
sort(permutations,fact);
cout<<"\nAFTER SORTING
\n";
for(i=0;i<fact;i++)
cout<<"\n"<<permutations[i];
getch();
}
No comments:
Post a Comment