-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
57 lines (52 loc) · 1.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ibohonos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/31 19:53:08 by ibohonos #+# #+# */
/* Updated: 2017/11/07 16:51:47 by ibohonos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_numb(int i, int minus)
{
int j;
j = 1;
if (minus == 1)
j++;
while (i > 9)
{
i /= 10;
j++;
}
return (j);
}
char *ft_itoa(int n)
{
int i;
int minus;
char *a;
minus = 0;
if (n < 0)
{
minus = 1;
n *= -1;
}
if (n == -2147483648)
return (ft_strdup("-2147483648"));
i = ft_count_numb(n, minus);
if ((a = (char *)malloc(sizeof(char) * i + 1)) == NULL)
return (NULL);
a[i--] = '\0';
while (n > 9)
{
a[i--] = (n % 10) + '0';
n /= 10;
}
a[i] = n + '0';
if (minus == 1)
a[--i] = '-';
return (a);
}