Untitled
unknown
plain_text
3 years ago
1.1 kB
4
Indexable
Never
/* ** EPITECH PROJECT, 2022 ** str_to_array_simple ** File description: ** str */ #include "my.h" int count_char(char *str, char c) { int i = 0; int count = 0; for (i = 0; str[i] != '\0'; i++) if (str[i] == c) count++; return count; } char **my_str_to_array_simple(char *str, char separator) { char **array = NULL; int i = 0; int j = 0; int k = 0; int size = 0; if (!str) return NULL; size = count_char(str, separator); array = malloc(sizeof(char *) * (size + 1)); for (i = 0; str[i] != '\0'; i += 1) { if (str[i] == separator) { array[j] = malloc(sizeof(char) * (i - k + 1)); for (int l = 0; l < i - k; l += 1) array[j][l] = str[k + l]; array[j][i - k] = '\0'; k = i + 1; j += 1; } } array[j] = malloc(sizeof(char) * (i - k + 1)); for (int l = 0; l < i - k; l += 1) array[j][l] = str[k + l]; array[j][i - k] = '\0'; array[j + 1] = NULL; return array; }