Print all the permutations of a string in C

The algorithm basically works on this logic:

All permutations of a string X is the same thing as all permutations of each possible character in X, combined with all permutations of the string X without that letter in it.

That is to say, all permutations of “abcd” are

  • “a” concatenated with all permutations of “bcd”
  • “b” concatenated with all permutations of “acd”
  • “c” concatenated with all permutations of “bad”
  • “d” concatenated with all permutations of “bca”

This algorithm in particular instead of performing recursion on substrings, performs the recursion in place on the input string, using up no additional memory for allocating substrings. The “backtracking” undoes the changes to the string, leaving it in its original state.

Leave a Comment