-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
62 lines (56 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gantonio <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/25 17:04:40 by gantonio #+# #+# */
/* Updated: 2021/05/30 12:52:42 by gantonio ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_signal(int n)
{
if (n < 0)
return (-1);
return (1);
}
static int ft_len(int n)
{
int size;
size = 0;
if (n <= 0)
size++;
while (n != 0)
{
n = n / 10;
size++;
}
return (size);
}
char *ft_itoa(int n)
{
char *ptr;
int i;
int status;
long n2;
n2 = n;
status = ft_signal(n2);
n2 *= status;
i = ft_len(n);
ptr = malloc(sizeof(char) * (i + 1));
if (!ptr)
return (NULL);
ptr[i--] = '\0';
if (n2 == 0)
ptr[i] = '0';
while (n2 > 0)
{
ptr[i--] = (n2 % 10) + '0';
n2 /= 10;
}
if (status == -1)
ptr[i] = '-';
return (ptr);
}