-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
60 lines (55 loc) · 1.41 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tekim <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/06 20:58:52 by tekim #+# #+# */
/* Updated: 2021/05/09 11:22:36 by tekim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_intlen(long n)
{
size_t i;
i = 0;
if (n == 0)
return (1);
if (n < 0)
{
n *= -1;
i++;
}
while (n)
{
n /= 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
char *ret;
size_t size;
long tmp;
tmp = n;
size = ft_intlen(tmp);
if (!(ret = (char *)malloc(sizeof(char) * size + 1)))
return (0);
ret[size--] = '\0';
if (tmp == 0)
ret[0] = '0';
if (tmp < 0)
{
tmp *= -1;
ret[0] = '-';
}
while (tmp)
{
ret[size] = tmp % 10 + '0';
tmp /= 10;
size--;
}
return (ret);
}