-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
87 lines (79 loc) · 1.88 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: galemair <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/03 16:14:27 by galemair #+# #+# */
/* Updated: 2018/04/04 12:48:51 by galemair ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_intmin(void)
{
char *str;
if ((str = malloc(sizeof(char) * 12)) == NULL)
return (NULL);
str[0] = '-';
str[1] = '2';
str[2] = '1';
str[3] = '4';
str[4] = '7';
str[5] = '4';
str[6] = '8';
str[7] = '3';
str[8] = '6';
str[9] = '4';
str[10] = '8';
str[11] = '\0';
return (str);
}
static int ft_get_size(int n, int neg)
{
int len;
len = 0;
if (neg == -1)
len++;
if (n == 0)
return (1);
while (n > 0)
{
len++;
n /= 10;
}
return (len);
}
static char *ft_fill(int n, char *str, int len, int neg)
{
str[len] = '\0';
if (n == 0)
str[--len] = '0';
while (n > 0)
{
len--;
str[len] = (n % 10) + '0';
n /= 10;
}
if (neg == -1)
str[0] = '-';
return (str);
}
char *ft_itoa(int n)
{
int len;
char *str;
int neg;
neg = 1;
if (n == -2147483648)
return (ft_intmin());
if (n < 0)
{
neg = -1;
n *= -1;
}
len = ft_get_size(n, neg);
if ((str = malloc(sizeof(char) * (len + 1))) == NULL)
return (NULL);
return (ft_fill(n, str, len, neg));
}