This repository has been archived on 2026-05-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
prog-103-p-02-2030/Digby_Real_Estate/Fundamentals/replace.c
T
2026-02-02 04:52:09 +01:00

71 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
char *replace(char *str, char *sub){
//clc taille sub
//nbr * x taille sub
if(str == NULL || sub == NULL) return NULL;
//clc taille sub
int index = 0;
while (*(sub + index)) 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 + 1) * sizeof(char));
if (rslt == NULL) return NULL;
index = 0;
index_str = 0;
int index_sub = 0;
//parcours total
while(*(str + index_str)){
//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);
index_sub ++;
index ++;
}
}
else{
*(rslt + index) = *(str + index_str);
index ++;
}
index_str ++;
}
*(rslt + index) = '\0';
return rslt;
}
int main(){
char *test =replace("Hello *", "World!"); // Hello World!
printf("%s\n", test);
free(test);
//printf("ici");
test = replace("*H*e*l*l*o*", "BIM"); // BIMHBIMeBIMlBIMlBIMoBIM
printf("%s\n", test);
free(test);
}