954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Permutations Of A Number Or String

By harshchandra on Nov 21st, 2004 7:12 pm

This program generates different combinations of a number or a string as given by the user.just a simple implementation of arrays.

/////////////////////////////////////////////////////////////////
//////     This program will generate all the possible ///////
/////        arrangement of the string or number      //////
//////////////////////////////////////////////////////////////


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


///////////////////////////////////////////////////////
/////////    Programmer : Harsh Chandra    ///////
/////////////////////////////////////////////////////



	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 0;
	}

	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;
	}

Very good program i was really needing it
thanx a lot

anurag_wizards
Newbie Poster
4 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Thanks the code has helped me a lot

muraya
Newbie Poster
3 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

# include - is nonstandard
#include - is nonstandard
void main() - is incorrect
ch=(char*)malloc(20) - the cast is unnecessary, you should check the return value
clrscr(); - nonstandard function
gets(ch); - never use gets()!
return 0; - returning a value from a void function?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

i dont found the program.where is it?

ainakya
Newbie Poster
1 post since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

/* Non-recursive Permutation generator
By : Mudit Aggarwal */

#include
#include
void main()
{
/* String */
char ch[]="123";

char temp;
int len = strlen(ch), count=1;
int i, start=len-2, end=len-1;

// Finds permutation count
for(i=1; i<=len; i++)
count *= i;

for(i=0; i

muditAggarwal
Newbie Poster
6 posts since Apr 2010
Reputation Points: 9
Solved Threads: 1
 
||=== perm, Debug ===| |warning: return type of 'main' is not `int'| ||=== Build finished: 0 errors, 1 warnings ===|

i wonder why your program gives me warning....

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Default return type of main is int. So, its throwing a warning as it is void in program. ( Sorry..my bad). however, program will run.

Make it :
int main()
{
// code
return 1;
}

muditAggarwal
Newbie Poster
6 posts since Apr 2010
Reputation Points: 9
Solved Threads: 1
 

okay. but don't do it again.

:)

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Mudit agarwal I implemented your program in CSharp it works for ok for abc but not for 'abcd' or 'abcde'. if abcd isgiven as input all i get is 12 strings instead of 24. The recursive approach is more cleaner and modular.

kawareness1
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

the entire thread, starting from the very first snippet by OP, is crap.

code snippets should not be allowed to be created by anyone with less than 100 posts.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

I agree, this is not up to snippet level, overall.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

unfortunately, the reality is that the snippet quality around here tends to be amateurish if not downright garbage.

so it actually IS "up to snippet level".

and that's the real problem

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

permutation in increasing order like - for {a,t,c,s,w,i} the permutations will be

acis
acit
aciw..so on

scuba-duba
Newbie Poster
2 posts since May 2010
Reputation Points: 7
Solved Threads: 0
 

need a code somewhat like this

set {a,t,c,i,w,s}
need to find all possible permutations of length 4 out of the above 6 chars,and each of the permutations must be in alphabetical order.

scuba-duba
Newbie Poster
2 posts since May 2010
Reputation Points: 7
Solved Threads: 0
 

>> need a code somewhat like this

Too bad for you. Closed

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You