-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
66 lines (61 loc) · 1.01 KB
/
ft_itoa.c
File metadata and controls
66 lines (61 loc) · 1.01 KB
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
61
62
63
64
65
66
#include "libft.h"
static char *ft_malloc_str(int n)
{
int sign;
int len;
int nbr;
char *str;
sign = (n >= 0) ? 1 : -1;
len = (n >= 0) ? 1 : 2;
nbr = n * sign;
while (nbr /= 10)
len++;
if (!(str = (char *)malloc((len + 1) * sizeof(char))))
return (0);
if (sign == -1)
str[0] = '-';
str[len] = '\0';
return (str);
}
static char *ft_convert_min_int()
{
char *str;
char *min_int;
int i;
i = 0;
min_int = "-2147483648";
if (!(str = (char *) malloc(12 * sizeof(char))))
return (0);
while (i < 11)
{
str[i] = min_int[i];
i++;
}
str[i] = '\0';
return (str);
}
char *ft_itoa(int n)
/*The string representing the integer. NULL if the allocation fails. */
{
char *str;
int i;
int pow;
int nbr;
str = ft_malloc_str(n);
if (str == 0)
return(0);
if (n == -2147483648)
return (ft_convert_min_int());
i = (n >= 0) ? 0 : 1;
n = (n >= 0) ? n : -n;
nbr = n;
pow = 1;
while (n /= 10)
pow = pow * 10;
while (pow > 0)
{
str[i++] = ((nbr / pow) % 10) + '0';
pow = pow / 10;
}
return (str);
}