Permutation of any word short and fast without pointers

comwizz comwizz is offline Offline Nov 18th, 2005, 2:54 pm |
0
Heres a precise and short way to permute any word without pointers
Quick reply to this message  
C Syntax
  1. //Permutation of any given word
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. void bubblesort(char a[],int n)
  6. {
  7. int i,j;
  8. char temp;
  9. for(i=0;i<n;i++)
  10. {
  11. for(j=0;j<n-i-1;j++)
  12. {
  13. if(a[j]>a[j+1])
  14. {
  15. temp=a[j];
  16. a[j]=a[j+1];
  17. a[j+1]=temp;
  18. }
  19. }
  20. }
  21. return;
  22. }
  23. factorial(int j)
  24. {
  25. int i,factorial=1;
  26. for(i=1;i<=j;i++)
  27. {
  28. factorial=factorial*i;
  29. }
  30. return(factorial);
  31. }
  32. char a[10];
  33. void main()
  34. {
  35. int o,n,i,j=0,k,right,left,recursion=0,product=1,count[4]={0};
  36. char temp;
  37. clrscr();
  38. printf("Enter any word\n");
  39. gets(a);
  40. n=strlen(a);
  41. /*Bubblesort the entire word*/
  42. bubblesort(a,n);
  43. /*Count the number of recursions of each character*/
  44. for(i=0;i<n;i++)
  45. {
  46. if(a[i]==a[i+1])
  47. {
  48. recursion++;
  49. count[j]++;
  50. if(a[i+1]!=a[i+2])
  51. {
  52. j++;
  53. }
  54. }
  55. }
  56. if(recursion==0)
  57. {
  58. o=factorial(n);
  59. }
  60. /*product of factorial of each recursion*/
  61. else
  62. {
  63. for(i=0;i<(n/2);i++)
  64. {
  65. product=product*factorial(count[i]+1);
  66. }
  67. o=(factorial(n)/product);
  68. }
  69. for(j=0;j<o;j++)
  70. {
  71. printf("%s\n",a);
  72. k=n-1;
  73. /*Take the descending order string of largest length starting from
  74. the right end of the array*/
  75. while(k>0&&a[k]>=a[k+1])
  76. {
  77. k--;
  78. }
  79. left=k+1;
  80. right=n-1;
  81. /* Sorting the descending string in ascending order*/
  82. while(left<=right)
  83. {
  84. temp=a[left];
  85. a[left++]=a[right];
  86. a[right--]=temp;
  87. }
  88. i=k+1;
  89. /*Comparing it with the element before the first element of the smaller
  90. string extracted*/
  91. while(a[k]>=a[i])
  92. i++;
  93. /*Swapping the first element larger than a[i], in the smaller substring
  94. arranged in ascending order ,with a[i]*/
  95. temp=a[k];
  96. a[k]=a[i];
  97. a[i]=temp;
  98. if(j%44==0)
  99. getch();
  100. }
  101. printf("\nNo of permutations : %d",o);
  102. getch();
  103. }

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC