-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
80 lines (73 loc) · 1.55 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
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yettabaa <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/07 23:49:05 by yettabaa #+# #+# */
/* Updated: 2022/11/03 22:36:01 by yettabaa ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int nb_n(int n)
{
long i;
long nb;
i = 0;
nb = n;
if (nb == 0)
return (1);
if (nb < 0)
{
nb *= -1;
i++;
}
while (nb > 0)
{
nb = nb / 10;
i++;
}
return (i);
}
static int pow_10(int n)
{
int i;
int r;
i = 1;
r = 1;
if (n < 0)
i++;
while (i < nb_n(n))
{
r *= 10;
i++;
}
return (r);
}
char *ft_itoa(int n)
{
long i;
long pow;
long nb ;
char *str;
nb = n;
pow = pow_10(n);
str = (char *)malloc(nb_n(n) + 1);
if (!str)
return (NULL);
i = 0;
if (nb < 0)
{
str[i++] = '-';
nb *= -1;
}
while (i < nb_n(n))
{
str[i++] = (nb / pow) + 48;
nb = nb - (nb / pow * pow);
pow /= 10;
}
str[i] = '\0';
return (str);
}