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 03:36:21 +01:00

74 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
char *replace(char *str, char *sub){
//clc taille sub
//nbr * x taille sub
int len_fin = 0;
//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;
while (*(sub + index)) index ++;
//longueur finale
len_fin += nb_etoile * index;
char *rslt = malloc(len_fin);
if (rslt == NULL) return NULL;
index = 0;
int 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) == '*'){
//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 ++;
}
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);
}*/