Skip to content

Instantly share code, notes, and snippets.

@jxcodetw
Created June 1, 2017 05:27
Show Gist options
  • Save jxcodetw/bb9c4d5f9d62f6b4d6a4e4a1492da718 to your computer and use it in GitHub Desktop.
Save jxcodetw/bb9c4d5f9d62f6b4d6a4e4a1492da718 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
void swap(char* a, char* b) {
char t = *a;
*a = *b;
*b = t;
}
void perm(char* head, int i, int n) {
int j;
if (i+1 == n) {
puts(head);
} else {
for(j=i;j&lt;n;++j) {
swap(head+i, head+j);
perm(head, i+1, n);
swap(head+i, head+j);
}
}
}
char buf[] = "abc";
int main(int argc, char* argv[]) {
perm(buf, 0, strlen(buf));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment