Permutations Of A Number Or String

harshchandra -2 Tallied Votes 249 Views Share

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

jephthah commented: this is pure dogshit. -1
/////////////////////////////////////////////////////////////////
//////     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;
	}
anurag_wizards 0 Newbie Poster

Very good program i was really needing it
thanx a lot

muraya 0 Newbie Poster

Thanks the code has helped me a lot

Dave Sinkula 2,398 long time no c Team Colleague

# include<alloc.h> - is nonstandard
#include<conio.h> - 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?

ainakya 0 Newbie Poster

i dont found the program.where is it?

muditAggarwal -1 Newbie Poster
/* Non-recursive Permutation generator
 By : Mudit Aggarwal  */

#include<stdio.h>
#include<string.h>
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<count; i++)
        {
                //swap characters
                temp=ch[end];
                ch[end]=ch[start];
                ch[start]=temp;

                printf("%s\n",ch);

                start--;
                end--;
                if(start<0)
                {
                        start=len-2;
                        end=len-1;
                }
        }

        getchar();
}
jephthah commented: neg rep for bumping a 5 year old thread, and posting snippet without code tags. the fact that you used void main makes me doubt it even works. can't say i care to find out. -1
jephthah 1,888 Posting Maven

||=== perm, Debug ===|
|warning: return type of 'main' is not `int'|
||=== Build finished: 0 errors, 1 warnings ===|

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

muditAggarwal -1 Newbie Poster

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

jephthah 1,888 Posting Maven

okay. but don't do it again.

:)

kawareness1 0 Newbie Poster

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.

jephthah 1,888 Posting Maven

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.

Adak 419 Nearly a Posting Virtuoso

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

jephthah 1,888 Posting Maven

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

scuba-duba -3 Newbie Poster

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

acis
acit
aciw..so on

scuba-duba -3 Newbie Poster

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.

mvmalderen commented: Bump bump bump, only for a stupid gimmethcodez request. -2
jonsca commented: Nope -1
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster

>> need a code somewhat like this

Too bad for you. Closed

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.