-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
49 lines (44 loc) · 1.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lrocigno <lrocigno@student.42sp.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/26 09:41:40 by lrocigno #+# #+# */
/* Updated: 2022/01/16 12:19:39 by lrocigno ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static long int set_ln(int n)
{
long int ln;
ln = n;
if (ln < 0)
ln *= -1;
return (ln);
}
char *ft_itoa(int n)
{
long int ln;
size_t n_sz;
size_t t_sz;
char *itoa;
ln = set_ln(n);
n_sz = ft_intlen(n);
t_sz = n_sz;
if (n < 0)
++t_sz;
itoa = ft_calloc(t_sz + 1, sizeof(*itoa));
if (!itoa)
return (NULL);
while (n_sz)
{
itoa[--t_sz] = (ln % 10) + 48;
ln /= 10;
--n_sz;
}
while (t_sz)
itoa[--t_sz] = '-';
return (itoa);
}