1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void anagramme(char *resultat, char *mot, int taille, int n)
{
char *copie_mot = malloc(taille);
{
int i;
for (i = 0; i < taille; i++)
copie_mot[i] = mot[i];
}
if (taille > 1)
{
int j, k;
int *deja_servi = calloc(taille, sizeof(int));
for (j = 0; j < taille; j++)
{
if (deja_servi[j] == 0)
{
for (k = 0; k < taille; k++)
{
if (copie_mot[k] == copie_mot[j])
deja_servi[k] = 1;
}
resultat[n - taille] = copie_mot[j];
{
int i;
for (i = 0; i < taille; i++)
mot[i] = copie_mot[i];
}
mot[j] = copie_mot[0];
mot[0] = resultat[n - taille];
anagramme(resultat, mot + 1, taille - 1, n);
}
}
free(deja_servi);
}
else
{
int i;
resultat[n - 1] = copie_mot[0];
for (i = 0; i < n; i++)
printf("%c", resultat[i]);
printf("\n");
}
free(copie_mot);
}
int main(void)
{
char mot[] = "papas";
size_t N = strlen(mot);
char *resultat = malloc(N);
anagramme(resultat, mot, N, N);
free(resultat);
return 0;
} |