-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
51 lines (46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alaulom <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/15 19:45:53 by alaulom #+# #+# */
/* Updated: 2014/11/15 19:54:22 by alaulom ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count(int i)
{
int j;
j = 0;
while (i /= 10)
j++;
return (j + 1);
}
char *ft_itoa(int i)
{
size_t size;
char *ret;
char *str;
size = count(i);
ret = (char *)malloc(sizeof(char) * (size + (i < 0 ? 1 : 0) + 1));
if (!ret)
return (NULL);
str = ret;
if (i == -2147483648)
return (ft_strcpy(str, "-2147483648"));
if (i < 0)
{
*str++ = '-';
i = -i;
}
str += size - 1;
*(str + 1) = 0;
while (size--)
{
*str-- = (char)(i % 10 + '0');
i /= 10;
}
return (ret);
}