Skip to content

Instantly share code, notes, and snippets.

@ershad
Created September 25, 2011 17:21
Show Gist options
  • Save ershad/1240862 to your computer and use it in GitHub Desktop.
Save ershad/1240862 to your computer and use it in GitHub Desktop.
String permutation
#include <stdio.h>
#include <string.h>
#define MAX 50
void permutations(char *string, int k, int m);
void swap(char *x, char *y);
int main(int argc, char *argv[])
{
// char string[MAX];
int k, m;
/* printf("\n Enter string: "); */
/* scanf("%s", string); */
permutations(argv[1], 0, strlen(argv[1]));
return 0;
}
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permutations(char *string, int k, int m)
{
int i;
if (k == m) {
printf("%s\n", string);
}
else
for (i = k; i < m; i++){
swap(&string[k], &string[i]);
permutations(string, k+1, m);
swap(&string[k], &string[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment