Permutations Of A Number Or String

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
harshchandra harshchandra is offline Offline Nov 21st, 2004, 9:12 am |
0
This program generates different combinations of a number or a string as given by the user.just a simple implementation of arrays.
Quick reply to this message  
C Syntax
  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.  
0
anurag_wizards anurag_wizards is offline Offline | Nov 21st, 2004
Very good program i was really needing it
thanx a lot
 
0
muraya muraya is offline Offline | Nov 26th, 2004
Thanks the code has helped me a lot
 
0
Dave Sinkula Dave Sinkula is offline Offline | Nov 26th, 2004
# 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?
 
0
ainakya ainakya is offline Offline | Feb 14th, 2005
i dont found the program.where is it?
 
 

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC