-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
66 lines (60 loc) · 1.6 KB
/
ft_itoa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvenance <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/03 15:02:31 by vvenance #+# #+# */
/* Updated: 2016/01/13 16:43:05 by vvenance ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_size(int i)
{
int c;
c = 0;
if (i < 0)
c++;
while (i)
{
c++;
i /= 10;
}
return (c);
}
static char *absolu(size_t v_absolu, size_t i, char *nb)
{
while (v_absolu)
{
nb[i--] = '0' + (v_absolu % 10);
v_absolu /= 10;
}
return (nb);
}
char *ft_itoa(int n)
{
char *nb;
size_t nbc;
size_t sign;
size_t v_absolu;
size_t i;
nbc = ft_size(n);
i = nbc - 1;
nb = (char *)malloc(sizeof(char) * (nbc) + 1);
if (nb == NULL)
return (NULL);
sign = ((n < 0) ? 1 : 0);
v_absolu = (unsigned)((n < 0) ? -n : n);
if (!v_absolu || v_absolu == 0)
{
nb[0] = '0';
nb[1] = '\0';
return (nb);
}
if (sign == 1)
nb[0] = '-';
nb = absolu(v_absolu, i, nb);
nb[nbc] = '\0';
return (nb);
}