-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
63 lines (57 loc) · 1.57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chanheki <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 01:20:51 by chanheki #+# #+# */
/* Updated: 2022/07/16 08:37:50 by chanheki ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_num_len(long long n)
{
int len;
len = 0;
if (n < 0)
{
++len;
n *= -1;
}
else if (n == 0)
return (1);
while (n != 0)
{
++len;
n /= 10;
}
return (len);
}
static char *storage_num(long long n, char *result)
{
if (n / 10 != 0)
result = storage_num(n / 10, result);
*result++ = n % 10 + '0';
*result = '\0';
return (result);
}
char *ft_itoa(int n)
{
char *result;
long long nb;
size_t size_nb;
nb = (long long)n;
size_nb = ft_num_len(nb);
result = (char *)malloc((sizeof(char) * size_nb) + 1);
if (!result)
return (NULL);
if (nb < 0)
{
*result++ = '-';
nb *= -1;
}
result = storage_num(nb, result);
result -= size_nb;
return (result);
}