Para quem precisar fazer permutações de caracteres, eis o código:
#include <stdio.h>
#include <string.h>
static unsigned int size = 0;
void permute(char *word, const unsigned int offset) {
unsigned register int i;
char t;
if (offset == (size - 1)) {
for (i = 0; i < size; i++) {
printf("%c", *(word + i));
}
printf("\\n");
return;
}
t = *(word + offset);
for (i = offset; i < size; i++) {
*(word + offset) = *(word + i);
*(word + i) = t;
permute(word, offset + 1);
*(word + i) = *(word + offset);
}
*(word + offset) = t;
}
int main(int argc, char **argv) {
char *word;
if (argc < 2) return 0;
if ((word = strdup(*(argv + 1))) == NULL) return 0;
size = strlen(word);
printf("Permutando \\"%s\\":\\n", *(argv + 1));
permute(word, 0);
return 0;
}
Arquivado como:Programação
c rules!
\o
C é coisa linda de Deus! hehe =D