-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
48 lines (43 loc) · 1.47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dieperei <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/07 19:04:46 by dieperei #+# #+# */
/* Updated: 2022/06/07 20:52:51 by dieperei ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_nbr_str_size(long int n)
{
if (n < 0)
return (1 + ft_nbr_str_size(n * -1));
if ((n / 10) == 0)
return (1);
return (1 + ft_nbr_str_size(n / 10));
}
static void ft_rec_itoa(long int n, char *nstr, int size)
{
if (n < 0)
{
nstr[0] = '-';
n *= -1;
}
if (n >= 10)
ft_rec_itoa((n / 10), nstr, (size - 1));
nstr[size] = (n % 10) + '0';
}
char *ft_itoa(int n)
{
char *nstr;
int size;
size = ft_nbr_str_size(n);
nstr = (char *)malloc((size + 1) * sizeof(char));
if (!nstr)
return (NULL);
nstr[size--] = '\0';
ft_rec_itoa(n, nstr, size);
return (nstr);
}