-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_itoa.c
executable file
·69 lines (63 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rel-fila <rel-fila@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/17 10:27:38 by rel-fila #+# #+# */
/* Updated: 2022/10/27 00:39:57 by rel-fila ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_len(int n)
{
size_t len;
len = 0;
if (n == -2147483648)
return (11);
else if (n < 0)
{
len++;
n = -n;
}
while (n > 0)
{
n = n / 10;
len++;
}
return (len);
}
static char *ft_create(int n, size_t len, char *tab)
{
while (n > 0)
{
tab[len--] = (n % 10) + '0';
n /= 10;
}
return (tab);
}
char *ft_itoa(int n)
{
char *tab;
size_t len;
size_t k;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
else if (n == 0)
return (ft_strdup("0"));
len = ft_len(n);
tab = (char *)malloc(sizeof(char) * (len + 1));
if (!tab)
return (0);
k = 0;
if (n < 0)
{
n = -n;
tab[0] = '-';
k = 1;
}
tab[len--] = '\0';
tab = ft_create(n, len, tab);
return (tab);
}