-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
executable file
·55 lines (50 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
43
44
45
46
47
48
49
50
51
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rlechapt <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/10 15:16:45 by rlechapt #+# #+# */
/* Updated: 2015/03/11 05:57:20 by rlechapt ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int ft_digit_count(int n)
{
int i;
i = 0;
if (n == 0)
return (1);
while (n)
{
n /= 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
int i;
int sign;
char *s;
unsigned int nb;
sign = 0;
nb = n;
if (n < 0)
{
sign = 1;
nb = -n;
}
i = ft_digit_count(n);
if ((s = (char *)ft_memalloc(i + sign + 1)) == NULL)
return (NULL);
while (i--)
{
s[i + sign] = (nb % 10) + '0';
nb /= 10;
}
if (sign == 1)
s[0] = '-';
return (s);
}