fork(1) download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. void print_permutation(char path[], int n)
  6. {
  7. int i = 0;
  8. for(i = 0; i < n; i++)
  9. printf("%c", path[i]);
  10. printf("\n");
  11. }
  12. void permutate(char a, int Na, char b, int Nb, char c, int Nc, int n, char path[], int index)
  13. {
  14. if(index == n )
  15. {
  16. if(Na != n - 3 && Nb == 1 && Nc == 2)
  17. path[index++] = a;
  18. else if(Na == n - 3 && Nb != 1 && Nc == 2)
  19. path[index++] = b;
  20. else if(Na == n - 3 && Nb == 1 && Nc != 2)
  21. path[index++] = c;
  22. print_permutation(path, index);
  23. return;
  24. }
  25. if(Na != n)
  26. {
  27. path[index] = a;
  28. permutate(a, Na + 1, b, Nb, c, Nc, n, path, index + 1);
  29. }
  30. if(Nb != 1)
  31. {
  32. path[index] = b;
  33. permutate(a, Na, b, Nb + 1, c, Nc, n, path, index + 1);
  34. }
  35. if(Nc != 2)
  36. {
  37. path[index] = c;
  38. permutate(a, Na, b, Nb, c, Nc + 1, n, path, index + 1);
  39. }
  40. }
  41. int main()
  42. {
  43. char a, b, c;
  44. printf("Enter 3 characters : \n");
  45. printf("a : ");
  46. scanf(" %c", &a);
  47. printf("b : ");
  48. scanf(" %c", &b);
  49. printf("c : ");
  50. scanf(" %c", &c);
  51.  
  52. int n;
  53. printf("Enter the size of string : ");
  54. scanf(" %d", &n);
  55.  
  56. char *path;
  57. path = (char *)malloc(n * sizeof(char));
  58.  
  59. permutate(a, 0, b, 0, c, 0, n, path, 0);
  60.  
  61. printf("\n");
  62. return 0;
  63. }
Success #stdin #stdout 0s 4300KB
stdin
Standard input is empty
stdout
Enter 3 characters : 
a : b : c : Enter the size of string :