Non recursive or no recursion implemetation of Permutations of a string

Please support our C advertiser: Programming Forums
Aug 6th, 2006
Views: 5,904
Thread Rating: 3 votes, 5.0000 average.
AddThis Social Bookmark Button
Hello friends here i am posting the non recursive or no recursion implementation of outputting Permutations of a string as compared to the recursive implementations previously posted.

Any feedbacks, constructive criticisms or comments are most welcome.
Last edited : May 21st, 2007.
c Syntax
  1. // permutations of any string inputted by user //
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #define display(X) printf( "\n%s", X );
  7. static int counter = 1 ;
  8.  
  9. /**
  10.  * This function is used to remove the trailing new line which is normally present at the end of the string accepted from the user
  11.  * using fgets( ). This function is very important since without this the newline character at the end of the input will be considered
  12.  * while drawing out permutations of the string which we dont want.
  13.  *
  14.  * @author ~s.o.s~
  15.  * @param input C style string from which the trailing newline has to be removed.
  16.  */
  17. void remove_newline( char* input )
  18. {
  19. char* p = 0 ;
  20. if( p = strrchr( input, '\n' ) )
  21. *p = '\0' ;
  22. }
  23.  
  24. /**
  25.  * This function is used to swap the two characters in the string array under consideration.
  26.  *
  27.  * @author ~s.o.s~
  28.  * @param a - character pointer pointing at the character to be swapped.
  29.  * @param b - character pointer pointing at the character to be swapped with.
  30.  */
  31. void swapPlaces (char* a, char* b)
  32. {
  33. char temp = *a;
  34. *a = *b;
  35. *b = temp;
  36. }
  37.  
  38. /**
  39.  * The algorithm used for permutations of the string is an adaptation of the "Countdown Quickperm algorithm"
  40.  * by Mr. Phillip Paul Fuchs. The credit for this algo goes to him.
  41.  *
  42.  * @author ~s.o.s~
  43.  * @param input which is the old C style string holding the string which has to be permuted
  44.  */
  45.  
  46. void wordPermutation (const char* input)
  47. {
  48. int string_length = strlen( input ) ;
  49.  
  50. if ( string_length == 0) // guard against no input
  51. return ;
  52.  
  53. int* p = (int*) malloc( string_length + 1 ) ;
  54. // start of naked block meant for initializing array P
  55. {
  56. int j ;
  57. for( j = 0; j <= string_length; ++j )
  58. p[j] = j ;
  59. }
  60. // end of naked block
  61.  
  62. char* tempBuffer = (char*) malloc( string_length + 1 ); // dont affect the original string, create temporary string.
  63. strcpy (tempBuffer, input);
  64. printf( "\n%s", tempBuffer ) ;
  65.  
  66. /// core algorithm begins
  67. int i = 1, j = 0;
  68. while(i < string_length)
  69. {
  70. p[i]--;
  71. j = i % 2 * p[i];
  72. swapPlaces( &tempBuffer[i], &tempBuffer[j] ) ;
  73. counter++ ;
  74. display(tempBuffer);
  75.  
  76. i = 1;
  77. while (!p[i])
  78. {
  79. p[i] = i;
  80. i++;
  81. }
  82. }
  83. /// core algorithm ends
  84.  
  85. free( tempBuffer ) ;
  86. free( p ) ;
  87.  
  88. printf( "\n\nThe number of permutations is %d\n\n", counter ) ;
  89. }
  90.  
  91. int main ( )
  92. {
  93. char buffer[BUFSIZ] = {'\0'} ;
  94. printf ("\nEnter the string whose permutation u want: ");
  95. fgets (buffer, BUFSIZ, stdin);
  96. remove_newline (buffer);
  97. wordPermutation (buffer);
  98. return 0;
  99. }
dileepkumar235 | Newbie Poster | Jan 18th, 2009
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char a[20],st_char;
static int i,j,k,n,st,ctr,main_ctr;

printf("Enter the string : ");
gets(a);

n=strlen(a);

if(n<=1)
{
printf("please enter a valid string ");
exit(0);
}

label :

for(i=0;i<=n-2;++i)
{
ctr=0;
printf("\n");
printf("%c",a[0]);
for(j=i+1;j<=n-1;j++)
{
printf("%c",a[j]);
ctr++;
}

if(ctr!=n-1)
//while(ctr!=n-1)
{
st=i+1;
for(k=1;k<=st-1;k++)
{
printf("%c",a[k]);
ctr++;
}
}
}

st_char=a[0];
for(i=0;i<=n-2;i++)
a[i]=a[i+1];

a[n-1]=st_char;

main_ctr++;

while(main_ctr<n)
goto label;

printf("Designed by Uday kumar and Dileep Basam ");
getch();
}  
~s.o.s~ | Failure as a human | Oct 29th, 2006
Changes made and now working for any kind and length of input -- thanks for letting me know it didn't work previously.  
Windsurfer | Newbie Poster | Oct 1st, 2006
It doesn't work for any string that has more than 3 characters  

Only community members can submit or comment on code snippets. You must register or log in to contribute.

Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:17 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC