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 | 161 | static int my_char_is_sep(char c, char *sep) | |
17 | { | ||
18 |
2/2✓ Branch 0 taken 385 times.
✓ Branch 1 taken 126 times.
|
511 | for (int i = 0; sep[i] != '\0'; i++) { |
19 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 350 times.
|
385 | if (c == sep[i]) |
20 | 35 | return 1; | |
21 | } | ||
22 | 126 | return 0; | |
23 | } | ||
24 | |||
25 | 10 | int count_words(char *str, char *sep) | |
26 | { | ||
27 | 10 | int n = 0; | |
28 | 10 | int do_it = 1; | |
29 | |||
30 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 10 times.
|
55 | for (int i = 0; str[i] != '\0'; i++) { |
31 |
4/4✓ Branch 1 taken 36 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 18 times.
|
45 | if (!my_char_is_sep(str[i], sep) && do_it) { |
32 | 18 | n++; | |
33 | 18 | do_it = 0; | |
34 | } | ||
35 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 36 times.
|
45 | if (my_char_is_sep(str[i], sep)) |
36 | 9 | do_it = 1; | |
37 | } | ||
38 | 10 | return n; | |
39 | } | ||
40 | |||
41 | 18 | int count_letters(char *str, char *sep, int *save) | |
42 | { | ||
43 | 18 | int n = 0; | |
44 | |||
45 | 18 | for (int i = *save; str[i] != '\0' | |
46 |
4/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 8 times.
|
54 | && !my_char_is_sep(str[i], sep); i++) { |
47 | 36 | (*save)++; | |
48 | 36 | n++; | |
49 | } | ||
50 | 18 | return n; | |
51 | } | ||
52 | |||
53 | 18 | int decale_save(int *save, char *str, char *sep) | |
54 | { | ||
55 | 18 | for (int i = *save; str[i] != '\0' | |
56 |
3/4✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 18 times.
|
27 | && my_char_is_sep(str[i], sep); i++) { |
57 | 9 | (*save)++; | |
58 | } | ||
59 | 18 | return 0; | |
60 | } | ||
61 | |||
62 | 18 | int fill_str(char *to_fill, int nb_letters, int save, char *source) | |
63 | { | ||
64 | 18 | int a = 0; | |
65 | |||
66 | 18 | for (int i = save - nb_letters; | |
67 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
|
54 | i < nb_letters + (save - nb_letters); i++) { |
68 | 36 | to_fill[a] = source[i]; | |
69 | 36 | a++; | |
70 | } | ||
71 | 18 | return 0; | |
72 | } | ||
73 | |||
74 | 10 | char **my_super_array(char *str, char *sep) | |
75 | { | ||
76 | 10 | int nb_words = count_words(str, sep); | |
77 | 10 | char **array = malloc(sizeof(char *) * (nb_words + 1)); | |
78 | 10 | int save = 0; | |
79 | 10 | int nb_letters = 0; | |
80 | |||
81 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
|
28 | for (int i = 0; i < nb_words; i++) { |
82 | 18 | decale_save(&save, str, sep); | |
83 | 18 | nb_letters = count_letters(str, sep, &save); | |
84 | 18 | array[i] = malloc(sizeof(char) * (nb_letters + 1)); | |
85 | 18 | fill_str(array[i], nb_letters, save, str); | |
86 | 18 | array[i][nb_letters] = '\0'; | |
87 | } | ||
88 | 10 | array[nb_words] = NULL; | |
89 | 10 | return array; | |
90 | } | ||
91 |