Created
September 25, 2011 17:21
-
-
Save ershad/1240862 to your computer and use it in GitHub Desktop.
String permutation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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