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

Recommended Answers

All 14 Replies

Wow - a full house of forums I've seen the same post on.

Huh? Whatcha mean?

>Huh? Whatcha mean?
He means that this question has been spammed on multiple website forums, such as Daniweb, cprogramming, etc...

Member Avatar for iamthwee

>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.

>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. ;)

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char a[20],st_char;
static int i,j,k,n,st,ctr,main_ctr;


printf("Enter the string : ");
gets(a);


n=strlen(a);


if(n<=1)
{
printf("please enter a valid string :) ");
exit(0);
}


label :


for(i=0;i<=n-2;++i)
{
ctr=0;
printf("\n");
printf("%c",a[0]);
for(j=i+1;j<=n-1;j++)
{
printf("%c",a[j]);
ctr++;
}


if(ctr!=n-1)
//while(ctr!=n-1)
{
st=i+1;
for(k=1;k<=st-1;k++)
{
printf("%c",a[k]);
ctr++;
}
}
}


st_char=a[0];
for(i=0;i<=n-2;i++)
a=a[i+1];


a[n-1]=st_char;


main_ctr++;


while(main_ctr<n)
goto label;


printf("Designed by Uday kumar and Dileep Basam ");
getch();
}
commented: No code tags, void main, pointless use of goto AND TWO YEARS TOO LATE! -5

Please use code tags!

Amen to Salem being a bot ;P

You posted it everywhere...pretty lame lol. What amazes me is how you managed to come up with code for permutations, but cannot write what appears to be a simple loop....

Chris

You missed to read about the forum posting expectations and proper code tagging.
I suggest you stick around and learn why your posted code is not a good example.

Not to mention the unmentionable, using gets().
A thoroughly deserved 0/10.

@dileepkumar
use the code tags.
and avoid the use of goto in the program.

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char a[50],st_char;
int i,j,k,n,st,ctr,main_ctr;

int fn_print()
{
 for(i=0;i<=n-2;++i)
	{
		ctr=0;  /*counter for printing the string */
		printf("\n");
		printf("%c",a[0]);
		for(j=i+1;j<=n-1;j++)
			{
				printf("%c",a[j]);
				ctr++;
			}

	if(ctr!=n-1)
	while(ctr!=n-1)
		{
			st=i+1;
			for(k=1;k<=st-1;k++)
			{
				printf("%c",a[k]);
				ctr++;
			}
		}
	}
st_char=a[0];
for(i=0;i<=n-2;i++)
a[i]=a[i+1];

a[n-1]=st_char;

main_ctr++;



}

sorry i am new to this site what are code tags exactly... ??

You missed to read about the forum posting expectations and proper code tagging.
I suggest you stick around and learn why your posted code is not a good example.

READ!

#include<stdio.h>

void swap(char*,int);
void gotoloop(char*,int);
int len;

main()
{
extern int len;
char ch[20];
int i,j,k,l=0,c;

printf("Enter the string\n");

while((c=getchar())!=EOF){ch[l]=c;l++;}
l--;
ch[l]='\0';
len=l;
gotoloop(ch,l);
printf("\n");
//return;
}

void gotoloop(char *ch,int l)
{
int i,k,j,z;
extern int len;
k=l;


if(l<=1)
return;

for(i=0;i<k;i++)
{
swap(ch,k);
l--;
gotoloop(ch,l);
l++;
for (j=2;j<=len;j++)
{
if(k==j)
{
printf("\n");
for(z=(len-1);z>=(j-1);z--) printf("%c",ch[z]);
}
}
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 the working code, if you have a problem you may use strlen function and gets instead of the getchar function that I have used. If you are using this code.
1. Enter the string
2. Hit enter.
3. press [ctrl]+D (to signify EOF)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.