This algorithm finds all permutations of the letters of a given string. It is a recursive algorithm using concept of backtracking.
Here is the source code of the C program to permute all letters of an input string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C program to find all permutations of letters of a string
/* Function to swap values at two pointers */
void swap (char *x, char *y)
temp = *x;
*x = *y;
*y = temp;
/* End of swap() */
/* Function to print permutations of string */
void permute(char *a, int i, int n)
if (i == n)
for (j = i; j <= n; j++)
swap((a + i), (a + j));
permute(a, i + 1, n);
swap((a + i), (a + j)); //backtrack
/* The main() begins */
printf("Enter a string: ");
n = strlen(a);
permute(a, 0, n - 1);
$ gcc permute.c -o permute $ ./permute Enter a string: SAN All possible permutations are: SAN SNA ASN ANS NAS NSA
Sanfoundry Global Education & Learning Series – 1000 C Programs.