-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
62 lines (56 loc) · 1.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amahla <amahla@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:28:54 by amahla #+# #+# */
/* Updated: 2022/05/02 12:28:56 by amahla ### ########.fr */
/* */
/* ************************************************************************** */
#include"libft.h"
static int nblen(unsigned int nb)
{
int i;
i = 0;
if (nb == 0)
return (1);
while (nb > 0)
{
nb = nb / 10;
i++;
}
return (i);
}
static void putnbr_itoa(unsigned int nb, int size, char *res)
{
if (nb >= 10)
putnbr_itoa(nb / 10, size - 1, res);
res[size] = nb % 10 + 48;
}
char *ft_itoa(int n)
{
unsigned int nb;
int size;
int sign;
char *res;
size = 0;
sign = 0;
if (n < 0)
{
sign++;
nb = n * -1;
}
else
nb = n;
size = nblen(nb);
res = malloc((size + sign + 1) * sizeof(char));
if (!res)
return (NULL);
putnbr_itoa(nb, size - 1 + sign, res);
if (sign)
res[0] = '-';
res[size + sign] = '\0';
return (res);
}