diff --git a/AcquiringLand/Fundamentals/bad_practice.c b/AcquiringLand/Fundamentals/bad_practice.c index 341a5ec..e1cb87a 100644 --- a/AcquiringLand/Fundamentals/bad_practice.c +++ b/AcquiringLand/Fundamentals/bad_practice.c @@ -3,7 +3,15 @@ int *division(int input, int by) { - int value = input / by; + + //int value = input / by; + if (by != 0){ + //int value = input / by; + } + else + return + + //int value = 0; int *ptr = &value; return ptr; } diff --git a/AcquiringLand/Fundamentals/insert_string.c b/AcquiringLand/Fundamentals/insert_string.c index 0bb36ab..1466620 100644 --- a/AcquiringLand/Fundamentals/insert_string.c +++ b/AcquiringLand/Fundamentals/insert_string.c @@ -5,21 +5,20 @@ char **insert_string(char **strings_array, int *array_size, char *insert_str, in char **rslt = malloc((*array_size + 1 )* sizeof(char *)); + if(rslt == NULL) return NULL; int index_clc = 0; + int index_clc2 = 0; //char *temp; - while (index_clc < *array_size){ + while (index_clc < *array_size + 1){ - if (index_clc == index){ - *(rslt + index_clc) = insert_str; - index_clc ++; - } + if (index_clc == index) *(rslt + index_clc) = insert_str; else{ - *(rslt + index_clc) = *(strings_array + index_clc); - } - + *(rslt + index_clc) = *(strings_array + index_clc2); + index_clc2 ++; + } /* if (index_clc >= index){ @@ -33,20 +32,23 @@ char **insert_string(char **strings_array, int *array_size, char *insert_str, in //*(strings_array + index) = insert_str; //free(temp); + *array_size = *array_size + 1; return rslt; } - +/* int main(){ char *tests[] = {"This", "is", "a", "long", "example"}; char *ins = "very"; int ted = 5; char **rslt = insert_string(tests, &ted, ins ,3); - int index = 0; - while (index < 6){ - printf("%s", *(rslt + index)); - index ++; + + if(rslt != NULL){ + + for (int i = 0; i < ted; i ++) printf("%s",rslt[i]); } + printf("\n"); + free (rslt); } - +*/ diff --git a/AcquiringLand/Fundamentals/itoa.c b/AcquiringLand/Fundamentals/itoa.c index 766a9a7..1239bec 100644 --- a/AcquiringLand/Fundamentals/itoa.c +++ b/AcquiringLand/Fundamentals/itoa.c @@ -23,7 +23,8 @@ char *my_itoa(int value){ int taille = get_number_length(value); char *rslt = malloc(taille + 1 * sizeof(char)); - + if (rslt == NULL) return NULL; + if (value < 0){ value = value * -1; *rslt = '-'; @@ -46,7 +47,7 @@ char *my_itoa(int value){ return rslt; } - +/* int main(){ printf("TESTS 1 \n"); printf("%i\n", get_number_length(7)); //1 @@ -66,4 +67,4 @@ int main(){ printf("%s\n", rslt); free(rslt); -} +}*/ diff --git a/AcquiringLand/Fundamentals/join_strings.c b/AcquiringLand/Fundamentals/join_strings.c index e69de29..77efdf9 100644 --- a/AcquiringLand/Fundamentals/join_strings.c +++ b/AcquiringLand/Fundamentals/join_strings.c @@ -0,0 +1,57 @@ +#include +#include +#include +char *join_strings(char *strings[], int count){ + + if (strings == NULL) return NULL; + + int tot = 0; + int index = 0; + //connaitre taille des str dans strings + for (int i = 0; i < count; i++){ + + index = 0; + //binf + while (strings[i][index]){ + tot ++; + index ++; + //printf("boucle inf ici"); + } + + } + + char *rslt = malloc((tot + count) * sizeof(char)); + if (rslt == NULL) return NULL; + + tot = 0; + + for (int i = 0; i < count; i ++){ + index = 0; + //binf + while (strings[i][index]){ + + *(rslt + tot) = strings[i][index]; + index ++; + tot ++; + } + if (i == count - 1) + *(rslt + tot) = '\0'; + else *(rslt + tot) = ' '; + tot ++; + } + //free(strings); + return rslt; +} +/* +int main(){ + + char *str[] = {"Hello", "from ", "Java", "swimming pool"}; + char *test = join_strings(str, 4); + printf("%s\n", test); + free (test); + + test = join_strings(NULL, 0); + printf("%s\n", test); + free(test); + +}*/ diff --git a/AcquiringLand/Fundamentals/leaks.c b/AcquiringLand/Fundamentals/leaks.c index fd08f34..78ae9c8 100644 --- a/AcquiringLand/Fundamentals/leaks.c +++ b/AcquiringLand/Fundamentals/leaks.c @@ -3,12 +3,13 @@ int **allocate_matrix(int rows, int cols) { - int **matrix = malloc(rows * sizeof(int *)); + int **matrix = malloc(rows * sizeof(int *));//creation du tableau X hauteur if (!matrix) return NULL; for (int i = 0; i < rows; i++) { - matrix[i] = malloc(cols * sizeof(int)); + matrix[i] = malloc(cols * sizeof(int));//creation des colonnes + //on free les cols si la matrice n'existe pas if (!matrix[i]) { for (int j = 0; j < i; j++) @@ -24,6 +25,9 @@ void free_matrix(int **matrix, int rows) { if (!matrix) return; + //ajout + for (int j = 0; j < rows; j ++) + free(matrix[j]); free(matrix); return; } @@ -47,5 +51,6 @@ int main(void) } free_matrix(matrix, rows); + //free(matrix); return EXIT_SUCCESS; } diff --git a/AcquiringLand/Fundamentals/magic_arrays.c b/AcquiringLand/Fundamentals/magic_arrays.c index e69de29..069fa11 100644 --- a/AcquiringLand/Fundamentals/magic_arrays.c +++ b/AcquiringLand/Fundamentals/magic_arrays.c @@ -0,0 +1,49 @@ +#include +//#include +int *compress_array(const int *input, int n, int *out_size){ + + if (input == NULL) return NULL; + + int taille = 1; + for (int i = 0; i < n -1; i++) if(input[i] != input[i + 1]) taille ++; + + *out_size = taille * 2; + + + int *rslt = malloc(*out_size * sizeof(int)); + if (rslt == NULL) return NULL; + + + int index_rslt = 0; + int index = 0; + while(index < n){ + + int ch_cons = 0; + int chiffre = input[index]; + + while (index < n && input[index] == chiffre){ + ch_cons ++; + index ++; + } + + rslt[index_rslt ++] = chiffre; + rslt[index_rslt ++] = ch_cons; + + } + + return rslt; + +} +/* +int main(){ + + int test[] = {3, 3, 3, 1, 1, 5, 5, 5, 5}; + int sortie = 6; + int *rs = compress_array(test, 9, &sortie); + + for(int i = 0; i < sortie; i++){ + printf("%i;", rs[i]); + } + free(rs); + +}*/ diff --git a/AcquiringLand/Fundamentals/test b/AcquiringLand/Fundamentals/test deleted file mode 100755 index 3e55db1..0000000 Binary files a/AcquiringLand/Fundamentals/test and /dev/null differ diff --git a/AcquiringLand/Fundamentals/trespass.c b/AcquiringLand/Fundamentals/trespass.c index dba1ca2..23e7c3b 100644 --- a/AcquiringLand/Fundamentals/trespass.c +++ b/AcquiringLand/Fundamentals/trespass.c @@ -51,6 +51,7 @@ int main(void) { return EXIT_FAILURE; } + printf("%s",res1); puts(res1); free(res1); return EXIT_SUCCESS;