-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
59 lines (54 loc) · 1.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abouramd <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 11:08:29 by abouramd #+# #+# */
/* Updated: 2022/10/19 11:56:07 by abouramd ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_ntos(char *p, int n, int index)
{
int stock;
stock = 0;
if (n < 0)
{
p[stock++] = '-';
n *= -1;
}
p[index] = '\0';
while (stock < index--)
{
p[index] = n % 10 + '0';
n /= 10;
}
return (p);
}
char *ft_itoa(int n)
{
int index;
char *p;
int stock;
index = 1;
stock = n;
if (stock < 0)
index++;
while (stock > 9 || stock < -9)
{
stock /= 10;
index++;
}
p = (char *)malloc(sizeof(char) * index + 1);
if (!p)
return (NULL);
stock = 0;
if (n == -2147483648)
{
ft_strlcpy(p, "-2147483648", index + 1);
return (p);
}
return (ft_ntos(p, n, index));
}