943,942 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 16742
  • C RSS
-2

Permutations Of A Number Or String

by on Nov 21st, 2004
This program generates different combinations of a number or a string as given by the user.just a simple implementation of arrays.
C Code Snippet (Toggle Plain Text)
  1. /////////////////////////////////////////////////////////////////
  2. ////// This program will generate all the possible ///////
  3. ///// arrangement of the string or number //////
  4. //////////////////////////////////////////////////////////////
  5.  
  6.  
  7. #include<stdio.h>
  8. #include<string.h>
  9. # include<alloc.h>
  10. #include<conio.h>
  11.  
  12.  
  13. ///////////////////////////////////////////////////////
  14. ///////// Programmer : Harsh Chandra ///////
  15. /////////////////////////////////////////////////////
  16.  
  17.  
  18.  
  19. void swap(char*,int);
  20. void gotoloop(char*,int);
  21.  
  22. void main()
  23. {
  24. char *ch;
  25. int i,j,k,l;
  26. ch=(char*)malloc(20);
  27. clrscr();
  28. printf("Enter the string\n");
  29. gets(ch);
  30.  
  31. l=strlen(ch);
  32. gotoloop(ch,l);
  33.  
  34. return 0;
  35. }
  36.  
  37. void gotoloop(char *ch,int l)
  38. {
  39. int i,k;
  40. k=l;
  41.  
  42. if(l<=1)
  43. return;
  44.  
  45.  
  46. for(i=0;i<k;i++)
  47. {
  48. swap(ch,k);
  49. l--;
  50. gotoloop(ch,l);
  51. l++;
  52. if(k==2)
  53. printf("\n%s ",ch);
  54. }
  55.  
  56. }
  57.  
  58.  
  59. void swap(char *ch,int r)
  60.  
  61. {
  62. char c;
  63. int i;
  64.  
  65. c=ch[r-1];
  66. for(i=r-1;i>0;i--)
  67. ch[i]=ch[i-1];
  68. ch[0]=c;
  69. }
  70.  
  71.  
Comments on this Code Snippet
Nov 21st, 2004
0

Re: Permutations Of A Number Or String

Very good program i was really needing it
thanx a lot
Newbie Poster
anurag_wizards is offline Offline
4 posts
since Nov 2004
Nov 26th, 2004
0

Re: Permutations Of A Number Or String

Thanks the code has helped me a lot
Newbie Poster
muraya is offline Offline
3 posts
since Nov 2004
Nov 26th, 2004
1

Re: Permutations Of A Number Or String

# 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?
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 14th, 2005
-1

Re: Permutations Of A Number Or String

i dont found the program.where is it?
Newbie Poster
ainakya is offline Offline
1 posts
since Feb 2005
Apr 3rd, 2010
-2

Re: Permutations Of A Number Or String

/* 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();
}
Newbie Poster
muditAggarwal is offline Offline
6 posts
since Apr 2010
Apr 3rd, 2010
0

Re: Permutations Of A Number Or String

Quote ...
||=== perm, Debug ===|
|warning: return type of 'main' is not `int'|
||=== Build finished: 0 errors, 1 warnings ===|
i wonder why your program gives me warning....
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 3rd, 2010
-1

Re: Permutations Of A Number Or String

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;
}
Newbie Poster
muditAggarwal is offline Offline
6 posts
since Apr 2010
Apr 3rd, 2010
0

Re: Permutations Of A Number Or String

okay. but don't do it again.

Last edited by jephthah; Apr 4th, 2010 at 12:00 am.
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 23rd, 2010
-1

Re: Permutations Of A Number Or String

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.
Newbie Poster
kawareness1 is offline Offline
1 posts
since Apr 2010
Message:
Previous Thread in C Forum Timeline: I/O file help
Next Thread in C Forum Timeline: getting data from usb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC