| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** my_str_to_word_array | ||
| 4 | ** File description: | ||
| 5 | ** Returns an array of words from a string (str) | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file my_str_to_word_array.c | ||
| 9 | * @brief The file containing the my_str_to_word_array function | ||
| 10 | * @author Nicolas TORO | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "my.h" | ||
| 14 | |||
| 15 | 4 | static void check_words_and_num(char const *str, int *sep, int *word) | |
| 16 | { | ||
| 17 | 2/2✓ Branch 0 taken 41 times. ✓ Branch 1 taken 4 times. | 45 | for (int i = 0; str[i] != '\0'; i++) { | 
| 18 | 4/4✓ Branch 1 taken 10 times. ✓ Branch 2 taken 31 times. ✓ Branch 4 taken 1 times. ✓ Branch 5 taken 9 times. | 41 | if ((my_char_is_alpha(str[i]) == 1 || my_char_is_num(str[i]) == 1) | 
| 19 | 2/2✓ Branch 0 taken 6 times. ✓ Branch 1 taken 26 times. | 32 | && *sep == 1) { | 
| 20 | 6 | *word = *word + 1; | |
| 21 | 6 | *sep = 0; | |
| 22 | } | ||
| 23 | 4/4✓ Branch 1 taken 10 times. ✓ Branch 2 taken 31 times. ✓ Branch 4 taken 9 times. ✓ Branch 5 taken 1 times. | 41 | if ((my_char_is_alpha(str[i]) == 0 && my_char_is_num(str[i]) == 0)) | 
| 24 | 9 | *sep = 1; | |
| 25 | } | ||
| 26 | 4 | return; | |
| 27 | } | ||
| 28 | |||
| 29 | 6 | static void count_letter(char const *str, int which_lettre, int *nbr_lettre) | |
| 30 | { | ||
| 31 | 6 | for (int i = which_lettre; str[i] != '\0' | |
| 32 | 4/4✓ Branch 0 taken 35 times. ✓ Branch 1 taken 3 times. ✓ Branch 3 taken 31 times. ✓ Branch 4 taken 4 times. | 38 | && (my_char_is_alpha(str[i]) != 0 | 
| 33 | 2/2✓ Branch 1 taken 1 times. ✓ Branch 2 taken 3 times. | 4 | || my_char_is_num(str[i]) != 0); i++) | 
| 34 | 32 | *nbr_lettre = *nbr_lettre + 1; | |
| 35 | 6 | } | |
| 36 | |||
| 37 | 6 | static void reset_nbr_letter(char const *str, | |
| 38 | int *which_lettre, int *nbr_lettre) | ||
| 39 | { | ||
| 40 | 6 | for (; str[*which_lettre] != '\0' | |
| 41 | 2/2✓ Branch 1 taken 6 times. ✓ Branch 2 taken 2 times. | 8 | && my_char_is_alpha(str[*which_lettre]) != 1 | 
| 42 | 4/4✓ Branch 0 taken 8 times. ✓ Branch 1 taken 3 times. ✓ Branch 3 taken 5 times. ✓ Branch 4 taken 1 times. | 17 | && my_char_is_num(str[*which_lettre]) != 1; | 
| 43 | 5 | *which_lettre = *which_lettre + 1); | |
| 44 | 6 | *nbr_lettre = 0; | |
| 45 | 6 | } | |
| 46 | |||
| 47 | 4 | static char *nbr_sep(char *str) | |
| 48 | { | ||
| 49 | 4 | int nbr_sep = 0; | |
| 50 | |||
| 51 | 9 | for (int i = 0; my_char_is_alpha(str[i]) != 1 | |
| 52 | 3/4✓ Branch 0 taken 5 times. ✓ Branch 1 taken 4 times. ✓ Branch 3 taken 5 times. ✗ Branch 4 not taken. | 9 | && my_char_is_num(str[i]) != 1; i++) | 
| 53 | 5 | nbr_sep++; | |
| 54 | 4 | return &str[nbr_sep]; | |
| 55 | } | ||
| 56 | |||
| 57 | 4 | static char **initialise_value(char **string, char const *str, | |
| 58 | int *sep, int *word) | ||
| 59 | { | ||
| 60 | char **array_of_word; | ||
| 61 | |||
| 62 | 4 | *string = nbr_sep(*string); | |
| 63 | 4 | check_words_and_num(str, sep, word); | |
| 64 | 4 | array_of_word = malloc(sizeof(char *) * ((*word) + 1)); | |
| 65 | 4 | return array_of_word; | |
| 66 | } | ||
| 67 | |||
| 68 | 4 | char **my_str_to_word_array(char const *str) | |
| 69 | { | ||
| 70 | 4 | int word = 0; | |
| 71 | 4 | int sep = 1; | |
| 72 | 4 | int nbr_lettre = 0; | |
| 73 | 4 | int which_lettre = 0; | |
| 74 | 4 | char *string = (char *)str; | |
| 75 | 4 | char **array_of_word = initialise_value(&string, str, &sep, &word); | |
| 76 | |||
| 77 | 2/2✓ Branch 0 taken 6 times. ✓ Branch 1 taken 4 times. | 10 | for (int elem = 0; elem < word; elem++) { | 
| 78 | 6 | count_letter(string, which_lettre, &nbr_lettre); | |
| 79 | 6 | array_of_word[elem] = malloc(sizeof(char) * (nbr_lettre + 1)); | |
| 80 | 2/2✓ Branch 0 taken 32 times. ✓ Branch 1 taken 6 times. | 38 | for (int chara = 0; chara < nbr_lettre; chara++) { | 
| 81 | 32 | array_of_word[elem][chara] = string[which_lettre]; | |
| 82 | 32 | which_lettre++; | |
| 83 | } | ||
| 84 | 6 | array_of_word[elem][nbr_lettre] = '\0'; | |
| 85 | 6 | reset_nbr_letter(string, &which_lettre, &nbr_lettre); | |
| 86 | } | ||
| 87 | 4 | array_of_word[word] = NULL; | |
| 88 | 4 | return array_of_word; | |
| 89 | } | ||
| 90 |