-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
46 lines (41 loc) · 1.27 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aelphias <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/29 17:26:31 by aelphias #+# #+# */
/* Updated: 2019/10/21 16:12:05 by aelphias ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int num_size(int nb)
{
int sz;
sz = 1;
while (nb /= 10)
++sz;
return (sz);
}
char *ft_itoa(int n)
{
char *s;
int sz;
unsigned int buf;
sz = num_size(n);
buf = n;
if (n < 0)
{
buf = -n;
sz++;
}
if (!(s = ft_strnew(sz)))
return (NULL);
s[--sz] = buf % 10 + '0';
while (buf /= 10)
s[--sz] = buf % 10 + '0';
if (n < 0)
*(s + 0) = '-';
return (s);
}