DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   possible combination of a string (http://www.daniweb.com/forums/thread64728.html)

vijayr_singh Dec 13th, 2006 8:35 am
possible combination of a string
 
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

Salem Dec 13th, 2006 2:02 pm
Re: possible combination of a string
 
Wow - a full house of forums I've seen the same post on.

cscgal Dec 13th, 2006 4:50 pm
Re: possible combination of a string
 
Huh? Whatcha mean?

Narue Dec 13th, 2006 4:59 pm
Re: possible combination of a string
 
>Huh? Whatcha mean?
He means that this question has been spammed on multiple website forums, such as Daniweb, cprogramming, etc...

iamthwee Dec 13th, 2006 6:01 pm
Re: possible combination of a string
 
Quote:

Originally Posted by Narue (Post 289259)
>Huh? Whatcha mean?
He means that this question has been spammed on multiple website forums, such as Daniweb, cprogramming, etc...



It could also be interpretated that Salem has so much free time on his hands he can afford to join every c forum in existence. ;) Although he does spend a lot of time on times.co.uk website. How he can juggle the two I don't know. He he.

Narue Dec 13th, 2006 7:36 pm
Re: possible combination of a string
 
>It could also be interpretated that Salem has so much free time on his
>hands he can afford to join every c forum in existence.
Please. Everyone knows that Salem is a bot. ;)


All times are GMT -4. The time now is 6:47 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC