« Suite de Conway » : différence entre les versions

Contenu supprimé Contenu ajouté
→‎En C : avec gestion RAM
Ligne 102 :
{
const int nb_terms = argc > 1 ? atoi(argv[1]) : 10;
int max_length = 1024;
char s1[256], s2[256] = "1", *start, *stop;
char *s1 = malloc(max_length), *s2 = malloc(max_length);
 
strcpy(s1, s2"1");
for (int i = 0; i < nb_terms; ++i) {
strcpy(s1, s2);
puts(s1);
if (strlen(s1) > max_length / 2) {
max_length *= 2;
s1 = realloc(s1, max_length);
free(s2); s2 = malloc(max_length);
}
s2[0] = 0;
char *start = s1;
while (*start) {
char *stop = start + 1;
while (*stop == *start) ++stop;
sprintf(s2, "%s%td%c", s2, stop - start, *start);
start = stop;
++stop;
}
strcpy(s1, s2);
}
free(s1); free(s2);
return 0;
}