Header Ads Widget

Program to print all permutation of a string ..

program:-

#include<stdio.h>
#include<string.h>
void main()
{
void permutation(char *,int ,int);//function declaration
int length;
char str[100];
printf("Enter the string ");
scanf("%s",str);
length=strlen(str);
permutation(str,0,length-1);// Function calling
}
// function definition
void permutation(char *s,int i,int n)
{
static int count ;
int j;
char ch;
if(i==n)
{
count++;
printf("%d %s\n",count,s);
}
else
{
for(j=i;j<=n;j++)
{
ch=*(s+i);
*(s+i)=*(s+j);
*(s+j)=ch;
permutation(s,i+1,n);

// Backtracting

ch=*(s+i);
*(s+i)=*(s+j);
*(s+j)=ch;
}
}
}



Post a Comment

0 Comments