Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_str_nbr_base_unsigned_size_t | ||
4 | ** File description: | ||
5 | ** Returns the string conversion of | ||
6 | ** an unsigned size_t number (nbr) in specific base (base) | ||
7 | */ | ||
8 | /** | ||
9 | * @file my_str_nbr_base_unsigned_size_t.c | ||
10 | * @brief The file containing the my_str_nbr_base_unsigned_size_t function | ||
11 | * @author Nicolas TORO | ||
12 | */ | ||
13 | |||
14 | #include "my.h" | ||
15 | |||
16 | 5 | static char *put_str_nb(size_t nb, int len_nb, | |
17 | char const *base, int base_len) | ||
18 | { | ||
19 | 5 | char *nb_str = malloc(sizeof(char) * (len_nb + 1)); | |
20 | 5 | size_t figure_temp = nb; | |
21 | |||
22 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 5 times.
|
14 | for (int i = 0; i < len_nb; i++) { |
23 | 9 | nb_str[len_nb - i - 1] = base[figure_temp % base_len]; | |
24 | 9 | figure_temp = (figure_temp - (figure_temp % base_len)) / base_len; | |
25 | } | ||
26 | 5 | nb_str[len_nb] = '\0'; | |
27 | 5 | return nb_str; | |
28 | } | ||
29 | |||
30 | 5 | char *my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base) | |
31 | { | ||
32 | 5 | int len_nb = 1; | |
33 | 5 | size_t temp_nb = nbr; | |
34 | 5 | int base_len = 0; | |
35 | |||
36 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5 times.
|
55 | while (base[base_len] != '\0') { |
37 | 50 | base_len = base_len + 1; | |
38 | } | ||
39 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
|
9 | while ((temp_nb / base_len) != 0) { |
40 | 4 | len_nb = len_nb + 1; | |
41 | 4 | temp_nb = temp_nb / base_len; | |
42 | } | ||
43 | 5 | return put_str_nb(nbr, len_nb, base, base_len); | |
44 | } | ||
45 |