Salve a tutti, ho un problema per implementare una funzione replace come quella della classe string di C++.
Io ho scritto questo codice però mi dà qualche problema e sinceramente nn riesco a capire perchè. :master:codice:char *replace(char *w, int i, int j, char *s) { char *temp = malloc((strlen(w)+j-1)*sizeof(char)); int k=0; int a=0; while(k<i) { char t[2] = {w[k], 0}; strcat(temp, t); k++; } for(a=0; a<j;a++) { char tt[2] = {s[a], 0}; strcat(temp, tt); } k+=1; for(k; k<strlen(w);k++) { char ttt[2] = {w[k], 0}; strcat(temp,ttt); } return temp; }
La funzione dovrebbe funzionare in modo da sostituire j caratteri (i primi j di s) sulla stringa w a partire dall'indice i.
Però per esempio con w = aabbBC, s=bb, i=3 e j=2 mi restituisce aabbbBC invece di aabbbC, in pratica applicare questa produzione bB->bb sulla stringa w.
Grazie
Ciao ciao![]()

Rispondi quotando