Friday 15 April 2011

Finding all possible combinations of a string in c

In this Program we are going to find all the possible combinations of a given string....
Example output:
Enter the String:123
The possible Strings are
132
123
213
312
231
321

#include<stdio.h>
#include<conio.h>
#include<string.h>
void swap(int ,int);
char temp,a[20],b[20],c[20];
int h,e,l,z,i=0,j=1;
void main()
{
clrscr();
printf("Enter the string:\n");
gets(a);
l=strlen(a);
for(i=0;a[i]!='\0';i++)
{
b[i]=a[i];
c[i]=a[i];
}
i=0;
swap(i,j);
getch();
}
void swap(i,j)
{
while(z<l)
{
if(i==z)
{
if(z==l-1)
{
i=0;j=1;
}
else if(z==l-2)
{
i=z+1;j=0;
}
else
{
i++;
j=i+1;
}
}
if(j==z)
{
if(z==l-1)
j=0;
else
j++;
}
temp=a[i];
a[i]=a[j];
a[j]=temp;
puts(a);
i++;
j++;
if(j==l)
j=0;
if(i==l)
i=0;
e=strcmp(a,c);
if(e==0)
{
for(i=0;a[i]!='\0';i++)
a[i]=b[i];
h=0;
z++;
temp=a[h];
a[h]=a[z];
a[z]=temp;
puts(a);
for(i=0;a[i]!='\0';i++)
c[i]=a[i];
i=0;j=1;
}
}
}

No comments:

Post a Comment