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