Monday, 1 April 2013

Permutation of string (without repetition)

#include<stdio.h>
#include<conio.h>
void swap (char *x,char *y);
void permit(char *a,int i,int n);
int k=0;
main()
{
char a[]="ABCDE";
clrscr();
permit(a,0,4);
printf("\n%d",k);
getch();
return(0);
}

void swap (char *x,char *y)
{
char temp;
temp=*x;
*x=*y;
*y=temp;
}

void permit (char *a,int i,int n)
{
int j;
if(i==n)          {
printf("%s\t",a);    k++;}
else
{
for(j=i;j<=n;j++)
{
swap((a+i),(a+j));
permit(a,i+1,n);
swap((a+i),(a+j));
}
}
}
                         Download C program

No comments:

Post a Comment