SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
C program to find all permutations of a string
C program to find all permutations of a string
Output:
Enter the string to permute: abc
The permutations of input string 'abc' are:
abc
acb
bac
bca
cba
cab
Implementation:
#include
#include
#include
void swap(char *s,char *v) { char temp; temp=*s; *s=*v; *v=temp; } void permute(char *s,int i,int n) { int j; if(i==n) { printf("%s\n",s); } else { for(j=i;j<=n;j++) { swap((s+i),(s+j)); permute(s,i+1,n); swap((s+i),(s+j)); } } } int main(void) { char *s; int slen; s=(char*)malloc(sizeof(char)*1000); printf("Enter the string to permute:\n"); scanf("%s",s); slen=strlen(s); printf("The permutations of input string '%s' are:\n",s); permute(s,0,slen-1); return 0; }
PREVIOUS
NEXT
HOME