-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_itoa.c
58 lines (53 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smbaabu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/24 01:26:43 by smbaabu #+# #+# */
/* Updated: 2019/02/27 01:37:19 by smbaabu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_intlen(int n)
{
int i;
i = 0;
if (n == 0)
return (1);
if (n < 0)
{
n = -n;
i++;
}
while (n > 0)
{
n /= 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
char *ret;
size_t len;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
len = ft_intlen(n);
if ((ret = ft_strnew(len)) == NULL)
return (NULL);
if (n == 0)
ret[0] = '0';
if (n < 0)
{
ret[0] = '-';
n *= -1;
}
while (n > 0)
{
ret[--len] = (n % 10) + '0';
n /= 10;
}
return (ret);
}