-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
70 lines (63 loc) · 1.53 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaghlou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/14 21:14:54 by azaghlou #+# #+# */
/* Updated: 2022/10/25 02:35:47 by azaghlou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int lennn(long nn)
{
int i;
i = 1;
if (nn < 0)
{
nn *= -1;
i++;
}
while (nn >= 10)
{
nn = nn / 10;
i++;
}
return (i);
}
static char *ft_putzero(char *str)
{
str[0] = '0';
str[1] = '\0';
return (str);
}
char *ft_itoa(int n)
{
long nb;
char *aptx;
int i;
nb = n;
i = lennn(nb);
aptx = malloc(sizeof(char) * (lennn(nb) + 1));
if (!aptx)
return (NULL);
if (nb == 0)
return (ft_putzero(aptx));
if (nb < 0)
{
aptx[0] = '-';
nb = nb * -1;
}
aptx[i] = '\0';
while (nb > 0 && i--)
{
aptx[i] = ((nb % 10) + '0');
nb = nb / 10;
}
return (aptx);
}
// int main()
// {
// printf("%s",ft_itoa(0));
// }