-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
78 lines (72 loc) · 1.69 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aymoulou <marvin@42.f> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/19 18:11:36 by aymoulou #+# #+# */
/* Updated: 2021/11/09 16:25:26 by aymoulou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_intsize(long n)
{
size_t intsize;
int isneg;
intsize = 0;
isneg = 0;
if (n < 0)
{
intsize++;
isneg++;
n = -n;
}
while (n >= 1)
{
intsize++;
n /= 10;
}
return (intsize);
}
static char *ft_gen(char *rtn, long nbr, int len, int isneg)
{
if (nbr != 0)
rtn = malloc(sizeof(char) * (len + 1));
else
return (rtn = ft_strdup("0"));
if (!rtn)
return (0);
isneg = 0;
if (nbr < 0)
{
isneg++;
nbr = -nbr;
}
rtn[len] = '\0';
while (--len)
{
rtn[len] = (nbr % 10) + '0';
nbr /= 10;
}
if (isneg == 1)
rtn[0] = '-';
else
rtn[0] = (nbr % 10) + '0';
return (rtn);
}
char *ft_itoa(int n)
{
int len;
char *rtn;
long nbr;
int isneg;
nbr = n;
len = ft_intsize(nbr);
rtn = 0;
isneg = 0;
rtn = ft_gen(rtn, nbr, len, isneg);
if (!(rtn))
return (0);
return (rtn);
}