Hi friends...
I need to write a program in C which will print all the combinations of a string with non-repeating characters. Example: “Say” will have the following: S, a,y, Sa, Sy, aS,Sy, yS, ya, aSy,Sya,ySa and so on. The string length is not known. The string will be a command line argument to the program.
The code that i have goes below but its not giving me the desired output. the output that i m getting from this program is like
#############OUTPUT##################
Enter the string : say
sya
ysa
yas
ays
asy
say
##################################
#############DESIRED OUTPUT##################
Enter the string : say
s
a
y
sa
sy
as
ys
ay
ya
sya
ysa
yas
ays
asy
say
###########################################
###########PROGRAM################
#include<stdio.h>
#include<string.h>
#include<alloc.h>
#include<conio.h>
void swap(char*,int);
void gotoloop(char*,int);
void main()
{
char *ch;
int i,j,k,l;
ch=(char*)malloc(20);
//clrscr();
printf("Enter the string\n");
gets(ch);
l=strlen(ch);
gotoloop(ch,l);
return;
}
void gotoloop(char *ch,int l)
{
int i,k;
k=l;
if(l<=1)
return;
for(i=0;i<k;i++)
{
swap(ch,k);
l--;
gotoloop(ch,l);
l++;
if(k==2)
printf("\n%s ",ch);
}
}
void swap(char *ch,int r)
{
char c;
int i;
c=ch[r-1];
for(i=r-1;i>0;i--)
ch[i]=ch[i-1];
ch[0]=c;
}
This is not like a normal permutation of string.
Can anyone help me on this ASAP.
Thanks in advance