-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
60 lines (54 loc) · 1.62 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: knzeng-e <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/04/01 05:11:00 by knzeng-e #+# #+# */
/* Updated: 2016/04/01 05:19:25 by knzeng-e ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_zero_or_min(int number)
{
char *str;
if (number == 0)
str = ft_strdup("0");
else
str = ft_strdup("-2147483648");
return (str);
}
static char *str_size(char **str, int *not_neg, size_t *i)
{
*i = 0;
*not_neg = 1;
if (!((*str) = (char *)malloc(sizeof(char) * 12)))
return (NULL);
return (*str);
}
char *ft_itoa(int nbr)
{
int pos;
size_t i;
char *str_number;
if (!(str_size(&str_number, &pos, &i)))
return (NULL);
if (nbr == 0 || nbr == -2147483648)
return (ft_zero_or_min(nbr));
if (nbr < 0)
{
nbr = -nbr;
pos = 0;
}
while (nbr)
{
str_number[i] = (nbr) % 10 + '0';
nbr = nbr / 10;
i++;
}
if (!pos)
str_number[i++] = '-';
str_number[i] = '\0';
return (ft_strrev(str_number));
}