From 49213b1343bf3119e0df1aad04a4d2329fdbcf01 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 2 Feb 2026 04:52:09 +0100 Subject: [PATCH] arret --- Digby_Real_Estate/Fundamentals/count.c | 14 ++++--- .../Fundamentals/most_frequent.c | 19 +++++---- Digby_Real_Estate/Fundamentals/my_factor.c | 2 +- Digby_Real_Estate/Fundamentals/my_strapp.c | 15 ++++--- Digby_Real_Estate/Fundamentals/my_weirddup.c | 27 +++++++----- Digby_Real_Estate/Fundamentals/replace.c | 41 +++++++++---------- 6 files changed, 64 insertions(+), 54 deletions(-) diff --git a/Digby_Real_Estate/Fundamentals/count.c b/Digby_Real_Estate/Fundamentals/count.c index b5cef6a..4d52e8b 100644 --- a/Digby_Real_Estate/Fundamentals/count.c +++ b/Digby_Real_Estate/Fundamentals/count.c @@ -1,8 +1,10 @@ #include -//#include +#include int *count(int n){ + if (n < 1) return NULL; - int *p = malloc(n); + + int *p = malloc(n * sizeof(int)); if(p == NULL) return NULL; while (n != 0){ @@ -14,15 +16,15 @@ int *count(int n){ } /* int main(){ - int *rslt = count(5); + int *rslt = count(5123456); int index = 0; while(*(rslt + index)){ - printf("%i", *(rslt + index)); + printf("%i, ", *(rslt + index)); index ++; } free(rslt); -}*/ - +} +*/ diff --git a/Digby_Real_Estate/Fundamentals/most_frequent.c b/Digby_Real_Estate/Fundamentals/most_frequent.c index 85d010c..007f2dc 100644 --- a/Digby_Real_Estate/Fundamentals/most_frequent.c +++ b/Digby_Real_Estate/Fundamentals/most_frequent.c @@ -14,17 +14,21 @@ char most_frequent(const char *str){ //temp lettre //index //index_clc - + + if (str == NULL) return '\0'; char *cha = malloc(2 * sizeof(char)); //{ lettre, temp} - if (cha == NULL) return 0; + if (cha == NULL) return '\0'; *cha = 0; // lettre *(cha + 1) = 0; //lettre pendant le clc int *values = malloc (4 * sizeof(int)); - if(values == NULL) return 0 ; + if(values == NULL) { + free(cha); + return '\0' ; + } //{ max, temp max, index, index_clc} *values = 0; // Maximum @@ -60,7 +64,7 @@ char most_frequent(const char *str){ free(values); return *cha; } -/* + int main () { char test = most_frequent("WRYYYY!1!"); // 'Y' @@ -68,9 +72,6 @@ int main () { test = most_frequent("ORA ORA ORA!"); // 'O' printf("%c\n",test); - test = most_frequent("hello warudo!"); // 'l' + test = most_frequent('\0'); // 'l' printf("%c\n", test); - most_frequent("oRA ora oRa?"); // 'o' - - -}*/ +} diff --git a/Digby_Real_Estate/Fundamentals/my_factor.c b/Digby_Real_Estate/Fundamentals/my_factor.c index a7011d3..489f06f 100644 --- a/Digby_Real_Estate/Fundamentals/my_factor.c +++ b/Digby_Real_Estate/Fundamentals/my_factor.c @@ -28,7 +28,7 @@ int *my_factor(int n){ tmp = n; div = 2; - while (temps > 1) { + while (tmp > 1) { if (tmp % div == 0){ *(kebab + index) = div; index ++; diff --git a/Digby_Real_Estate/Fundamentals/my_strapp.c b/Digby_Real_Estate/Fundamentals/my_strapp.c index f18cabe..e0bb6c6 100644 --- a/Digby_Real_Estate/Fundamentals/my_strapp.c +++ b/Digby_Real_Estate/Fundamentals/my_strapp.c @@ -7,7 +7,8 @@ char *strdup(const char *s); void my_strapp(char *src, char **dest){ if (src == NULL || dest == NULL) return; - + if (*dest == NULL) return; + //taille src int index = 0; while (*(src + index)) index ++; @@ -17,10 +18,11 @@ void my_strapp(char *src, char **dest){ while (*(*dest + index_dest)) index_dest ++; //realloc des dest avec la taille de src - *dest = realloc(*dest, index + index_dest + 1); + char *new = realloc(*dest, (index + index_dest + 1) * sizeof(char)) ; //int *dest = realloc(*dest, index); - if (dest == NULL) return; + if(new == NULL) return; + *dest = new; index = 0; /*while(*(new + index_dest)){ @@ -42,19 +44,20 @@ void my_strapp(char *src, char **dest){ index ++; } - *(dest + index_dest + index + 1) = '\0'; + *(*dest + index_dest + index) = '\0'; } -/* + int main(){ char *src = strdup("World!"); char *dest = strdup("Hello, "); + //char *dest = '\0'; char **dest_ptr = &dest; my_strapp(src, dest_ptr); // Hello, World! printf("%s", *dest_ptr); free(src); free(dest); -}*/ +} diff --git a/Digby_Real_Estate/Fundamentals/my_weirddup.c b/Digby_Real_Estate/Fundamentals/my_weirddup.c index 04ca12a..533b4db 100644 --- a/Digby_Real_Estate/Fundamentals/my_weirddup.c +++ b/Digby_Real_Estate/Fundamentals/my_weirddup.c @@ -1,6 +1,8 @@ #include #include +char *strdup(const char *s); + char *my_weirddup(char *src){ if(src == NULL) return NULL; @@ -10,7 +12,7 @@ char *my_weirddup(char *src){ index ++; } - char *str = malloc(index); + char *str = malloc((index + 1) * sizeof(char)); if(str == NULL) return NULL; //printf("test"); @@ -29,7 +31,7 @@ char *my_weirddup(char *src){ } //min - if (lettre > 96 && lettre < 124) { + else if (lettre > 96 && lettre < 123) { if (lettre == 'z') lettre = 'a'; else lettre ++; } @@ -38,22 +40,27 @@ char *my_weirddup(char *src){ index ++; } - *(str + index + 1) = '\0'; + *(str + index) = '\0'; return str; } -//TODO MARCHE MAIS À COMPLETER int main(int args ,char **arg){ int index = 1; - char *rslt = ""; + char *rslt = NULL; + char *ic = NULL; while(index < args){ - rslt = my_weirddup(*(arg + index)); - printf("%s\n",rslt); - free(rslt); - index ++; + + ic = strdup(*(arg + index)); + rslt = my_weirddup(ic); + + if (rslt != NULL){ + printf("%s\n", rslt); + free(rslt); } - + free(ic); + index ++; + } } diff --git a/Digby_Real_Estate/Fundamentals/replace.c b/Digby_Real_Estate/Fundamentals/replace.c index 2dd5dff..ff776f0 100644 --- a/Digby_Real_Estate/Fundamentals/replace.c +++ b/Digby_Real_Estate/Fundamentals/replace.c @@ -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); -}*/ +}