Permutations of a String

Reply

Join Date: Mar 2008
Posts: 21
Reputation: theausum has a little shameless behaviour in the past 
Solved Threads: 1
theausum theausum is offline Offline
Newbie Poster

Permutations of a String

 
-1
  #1
Oct 27th, 2008
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4.  
  5. void permute(char *str,int l,int pos,int r);
  6. void swap(char &a,char &b);
  7. void print_string(char *str,int r);
  8.  
  9. int main()
  10. {
  11. char str[10]="";
  12. int l,r;
  13. printf("Enter The String : ");
  14. gets(str);
  15. l=strlen(str);
  16. printf("Enter The Number Of Places To permute on : ");
  17. scanf("%d",&r);
  18. printf("The Following Permuations are possible : \n\n");
  19. permute(str,l,1,r);
  20. getch();
  21. return 0;
  22. }
  23.  
  24. void permute(char *str,int l,int pos,int r)
  25. {
  26. //If lock position is on the next character
  27. //than the limit
  28. if(pos==r+1)
  29. {
  30. print_string(str,r); //print - these are the elements//
  31. printf(" ");
  32. return; //and return//
  33. }
  34. //true subscript of character in array is pos-1//
  35. for(int i=pos-1;i<=l-1;i++)
  36. {
  37. //swap the first letter with all next letters
  38. str[pos-1]=str[pos-1]+str[i]-(str[i]=str[pos-1]);
  39. permute(str,l,pos+1,r);
  40. //restore the swap{swap : a=a+b-(b=a)}
  41. str[pos-1]=str[pos-1]+str[i]-(str[i]=str[pos-1]);
  42. }
  43. }
  44.  
  45. void print_string(char *str,int r)
  46. {
  47. for(int i=0;i<r;i++)
  48. printf("%c",str[i]);
  49. }
Last edited by Ancient Dragon; Oct 27th, 2008 at 7:37 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC