arret
This commit is contained in:
@@ -6,39 +6,36 @@ char *replace(char *str, char *sub){
|
||||
//clc taille sub
|
||||
//nbr * x taille sub
|
||||
|
||||
int len_fin = 0;
|
||||
if(str == NULL || sub == NULL) return NULL;
|
||||
|
||||
//clc taille str
|
||||
int index = 0;
|
||||
int nb_etoile = 0;
|
||||
while( *(str + index)){
|
||||
if ( *(str + index) == '*') nb_etoile ++;
|
||||
|
||||
index ++;
|
||||
|
||||
}
|
||||
len_fin = index;
|
||||
|
||||
//clc taille sub
|
||||
index = 0;
|
||||
int index = 0;
|
||||
while (*(sub + index)) index ++;
|
||||
|
||||
//longueur finale
|
||||
len_fin += nb_etoile * index;
|
||||
int index_str = 0;
|
||||
int nb_etoile = 0;
|
||||
while( *(str + index_str)){
|
||||
|
||||
if ( *(str + index_str) == '*') nb_etoile ++;
|
||||
|
||||
index_str ++;
|
||||
}
|
||||
|
||||
int len_fin = index_str - nb_etoile + ( index * nb_etoile);
|
||||
|
||||
|
||||
char *rslt = malloc(len_fin);
|
||||
char *rslt = malloc((len_fin + 1) * sizeof(char));
|
||||
if (rslt == NULL) return NULL;
|
||||
|
||||
index = 0;
|
||||
int index_str = 0;
|
||||
index_str = 0;
|
||||
int index_sub = 0;
|
||||
//parcours total
|
||||
while(*(str + index_str)){
|
||||
|
||||
index_sub = 0;
|
||||
|
||||
//si le char est une etoile
|
||||
if (*(str + index_str) == '*'){
|
||||
index_sub = 0;
|
||||
//copie de sub dans rslt
|
||||
while(*(sub + index_sub)){
|
||||
*(rslt + index) = *(sub + index_sub);
|
||||
@@ -54,12 +51,12 @@ char *replace(char *str, char *sub){
|
||||
index_str ++;
|
||||
|
||||
}
|
||||
|
||||
*(rslt + index) = '\0';
|
||||
return rslt;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
@@ -70,4 +67,4 @@ int main(){
|
||||
test = replace("*H*e*l*l*o*", "BIM"); // BIMHBIMeBIMlBIMlBIMoBIM
|
||||
printf("%s\n", test);
|
||||
free(test);
|
||||
}*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user