-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathft_itoa.c
More file actions
90 lines (82 loc) · 1.92 KB
/
Copy pathft_itoa.c
File metadata and controls
90 lines (82 loc) · 1.92 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jperpect <jperpect@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/17 16:29:07 by jperpect #+# #+# */
/* Updated: 2024/11/29 14:38:19 by jperpct ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *nuber(int cont, int r, char *ret)
{
int rest;
char temp;
while (0 <= cont)
{
rest = r % 10;
temp = rest + '0';
ret[cont] = temp;
r = r / 10;
cont--;
}
return (ret);
}
static int conta(int n, int cont)
{
if (n == 0)
return (1);
while (n > 0)
{
cont++;
n = n / 10;
}
return (cont);
}
static char *lines(int n, char *ret, int neg, int cont)
{
if (-(n) == -2147483648)
{
ft_strlcpy(ret, "-2147483648", 12);
return (ret);
}
ret = nuber(cont - 1, n, ret);
if (neg == 1)
{
ret[0] = '-';
}
return (ret);
}
char *ft_itoa(int n)
{
int cont;
int neg;
char *ret;
cont = 0;
neg = 0;
if (n < 0)
{
cont = 1;
if (n == -2147483648)
cont = 11;
n = -(n);
neg = 1;
}
cont = conta(n, cont);
ret = (char *)ft_malloc((cont + 1) * sizeof(char), NULL);
if (ret == NULL)
return (NULL);
ret[cont] = '\0';
if (cont == 1)
cont = 1;
return (lines(n, ret, neg, cont));
}
// int main(int ac, char **av)
// {
// char *ok;
// ok =ft_itoa(-2147483648);
// printf("%s",ok);
// free(ok);
// }