-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
93 lines (85 loc) · 1.82 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
88
89
90
91
92
93
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aachhoub <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 17:15:07 by aachhoub #+# #+# */
/* Updated: 2022/10/18 09:50:36 by aachhoub ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_ilen(long nb)
{
size_t size;
long long i;
size = 0;
if (nb == 0)
size++;
if (nb < 0)
{
i = nb * -1;
size++;
}
else
i = nb;
while (i > 0)
{
i /= 10;
size++;
}
return (size);
}
static int ft_sign(long *nb, size_t *size)
{
if (*nb < 0)
{
*nb = *nb * -1;
*size = *size - 1;
return (-1);
}
return (1);
}
static char *ft_strrev(char *str, size_t len)
{
size_t i;
char c;
i = 0;
while (i < len)
{
c = str[i];
str[i] = str[len - 1];
str[len - 1] = c;
len--;
i++;
}
return (str);
}
char *ft_itoa(int nb)
{
size_t size;
size_t i;
int sign;
char *a;
long n;
n = nb;
i = 0;
size = ft_ilen(n);
a = (char *)malloc((size + 1) * sizeof(char));
if (!a)
return (0);
sign = ft_sign(&n, &size);
if (n == 0)
a[i++] = '0';
while (i < size)
{
a[i++] = (n % 10) + 48;
n /= 10;
}
if (sign == -1)
a[i++] = '-';
a[i] = '\0';
ft_strrev(a, ft_strlen(a));
return (a);
}