This is a C program to permute all the letters of an input string.
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Buy Computer Science Books
- Buy C Books
- Practice BCA MCQs