-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
79 lines (73 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jamendoe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/12 18:21:26 by jamendoe #+# #+# */
/* Updated: 2022/11/12 18:21:30 by jamendoe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_digits(int n)
{
int dn;
dn = 1;
if (n < 0)
dn++;
while (n / 10)
{
n = n / 10;
dn++;
}
return (dn);
}
static void ft_write(char *c, int n, int dn)
{
int i;
i = 1;
if (n < 0)
{
if (n == -2147483648)
{
c[dn--] = 8 + '0';
n = 214748364;
}
else
n *= -1;
c[0] = '-';
}
else
i = 0;
while (dn >= i)
{
c[dn] = n % 10 + 48;
n = n / 10;
dn--;
}
}
char *ft_itoa(int n)
{
char *c;
int dn;
dn = ft_digits(n);
c = (char *)malloc((dn + 1) * sizeof(*c));
if (!c)
return (NULL);
c[dn--] = '\0';
ft_write(c, n, dn);
return (c);
}
/*
Function name ft_itoa
Prototype char *ft_itoa(int n);
Turn in files -
Parameters n: the integer to convert.
Return value The string representing the integer.
NULL if the allocation fails.
External functs. malloc
Description Allocates (with malloc(3)) and returns a string
representing the integer received as an argument.
Negative numbers must be handled.
*/