fork(12) download
  1. /*
  2.  * Rishi Verma
  3.  * 19-Feb-2015
  4.  *
  5.  * http://w...content-available-to-author-only...s.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
  6.  */
  7. class PrintStringCombi {
  8.  
  9. static char s[] = { 'a', 'b', 'c' };
  10.  
  11. /* Function to swap values at two pointers */
  12. static void swap(int i, int j) {
  13. char temp = s[i];
  14. s[i] = s[j];
  15. s[j] = temp;
  16. }
  17.  
  18. /*
  19. * Function to print permutations of string This function takes three
  20. * parameters: 1. String 2. Starting index of the string 3. Ending index of
  21. * the string.
  22. */
  23. static void permute(int i) {
  24. int j;
  25. if (i == s.length - 1)
  26. System.out.println(s);
  27. else {
  28. for (j = i; j < s.length; j++) {
  29. swap(i, j);
  30. permute(i + 1);
  31. swap(i, j); // backtrack
  32. }
  33. }
  34. }
  35.  
  36. /* Driver program to test above functions */
  37. public static void main(String[] args) {
  38. permute(0);
  39. }
  40. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
abc
acb
bac
bca
cba
cab