-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
70 lines (62 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
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfilloux <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 10:29:27 by lfilloux #+# #+# */
/* Updated: 2021/11/05 17:43:11 by lfilloux ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_len(long nb)
{
int count;
count = 0;
if (nb < 0)
{
count ++;
nb = -nb;
}
while (nb >= 10)
{
count ++;
nb = nb / 10;
}
return (count);
}
static void ft_decrementation(char *dest, int len, long nb)
{
char *base;
base = "0123456789";
if (nb >= 10)
ft_decrementation(dest, len - 1, nb / 10);
dest[len] = base[nb % 10];
}
char *ft_itoa(int n)
{
long nb;
int len;
char *dest;
nb = (long)n;
len = ft_len(nb);
dest = (char *)malloc(sizeof(char) * (len + 2));
if (!dest)
return (NULL);
if (nb < 0)
{
dest[0] = '-';
nb = -nb;
}
ft_decrementation(dest, len, nb);
dest[len + 1] = '\0';
return (dest);
}
/*
int main(void)
{
printf("%s\n", ft_itoa(-2147483648));
return (0);
}
*/