This commit is contained in:
2026-02-02 03:36:21 +01:00
parent 2ae8d93f16
commit 6841949095
7 changed files with 365 additions and 23 deletions
+73
View File
@@ -0,0 +1,73 @@
#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);
}*/