954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

vijayr_singh
Newbie Poster
1 post since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Huh? Whatcha mean?

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>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 everyc 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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

#include
#include
#include
#include
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[i]=a[i+1];

a[n-1]=st_char;

main_ctr++;

while(main_ctr

dileepkumar235
Newbie Poster
13 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

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

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

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.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

ajay.krish123
Junior Poster in Training
90 posts since Nov 2008
Reputation Points: 6
Solved Threads: 9
 
#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++;



}
dileepkumar235
Newbie Poster
13 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 

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

dileepkumar235
Newbie Poster
13 posts since Jan 2009
Reputation Points: 5
Solved Threads: 0
 
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!

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 
#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)

siriussam
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You