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
| char * str_remplace (const char *s, unsigned int start, unsigned int length, const char *ct){
char *new_s = NULL;
if (s && ct) {
size_t size = strlen (s);
/* Tester start, length >= 0 est inutile, ils sont unsigned... */
if ( start + length <= size) {
/* Allocation de la nouvelle taille -> +1 pour le \0 de fin */
new_s = (char*)malloc (size - length + strlen (ct) + 1);
if (new_s) {
/* Copie du debut */
memmove (new_s, s, start);
/* Copie du remplacement */
memmove (new_s + start, ct, strlen (ct));
/* Copie de la fin -> +1 pour le '\0' */
memmove (new_s + (start + strlen (ct)),
s + (start + length),
strlen(s+start+length) + 1);
}
else {
fprintf (stderr, "Memoire insuffisante\n");
return NULL;
}
}
}
return new_s;
} |
Partager