Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2023 | ||
3 | ** my_str_nbr | ||
4 | ** File description: | ||
5 | ** Returns the string conversion of a number (nb) | ||
6 | */ | ||
7 | /** | ||
8 | * @file my_str_nbr.c | ||
9 | * @brief The file containing the my_str_nbr function | ||
10 | * @author Nicolas TORO | ||
11 | */ | ||
12 | |||
13 | #include "my.h" | ||
14 | |||
15 | 561 | static char *set_str_nb(int nb, int int_nb, int signe) | |
16 | { | ||
17 | 561 | char *nb_str = malloc(sizeof(char) * (int_nb + signe + 1)); | |
18 | 561 | int j = 0; | |
19 | 561 | int figure_temp = nb; | |
20 | |||
21 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 544 times.
|
561 | if (signe == 1) |
22 | 17 | nb_str[int_nb] = '-'; | |
23 |
2/2✓ Branch 0 taken 841 times.
✓ Branch 1 taken 561 times.
|
1402 | for (j = 0; j < int_nb; j++) { |
24 | 841 | nb_str[j] = 48 + (figure_temp % 10); | |
25 | 841 | figure_temp = (figure_temp - (figure_temp % 10)) / 10; | |
26 | } | ||
27 | 561 | nb_str[j + signe] = '\0'; | |
28 | 561 | return my_revstr(nb_str); | |
29 | } | ||
30 | |||
31 | 562 | char *my_str_nbr(int nb) | |
32 | { | ||
33 | 562 | int int_nb = 1; | |
34 | 562 | int temp_nb = nb; | |
35 | 562 | int signe = 0; | |
36 | |||
37 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 561 times.
|
562 | if (nb == -2147483648) |
38 | 1 | return "-2147483648"; | |
39 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 544 times.
|
561 | if (nb < 0) { |
40 | 17 | signe = 1; | |
41 | 17 | nb = -nb; | |
42 | } | ||
43 |
2/2✓ Branch 0 taken 280 times.
✓ Branch 1 taken 561 times.
|
841 | while ((temp_nb / 10) != 0) { |
44 | 280 | int_nb = int_nb + 1; | |
45 | 280 | temp_nb = temp_nb / 10; | |
46 | } | ||
47 | 561 | return set_str_nb(nb, int_nb, signe); | |
48 | } | ||
49 |