-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
executable file
·42 lines (38 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tseguier <tseguier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/11/25 16:22:49 by tseguier #+# #+# */
/* Updated: 2014/03/27 18:45:44 by jcoignet ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include "libft.h"
static size_t ft_compute_nbsize(int nb)
{
if (nb / 10 == 0)
return (1);
return (1 + ft_compute_nbsize(nb / 10));
}
char *ft_itoa(int n)
{
size_t size;
int neg;
char *nbstr;
neg = (n < 0) ? 1 : 0;
size = neg + ft_compute_nbsize(n);
nbstr = ft_strnew(size);
if (!nbstr)
return (NULL);
if (neg == 1)
nbstr[0] = '-';
while (size-- > (size_t)neg)
{
nbstr[size] = '0' + (char)((neg) ? (0 - n % 10) : (n % 10));
n /= 10;
}
return (nbstr);
}