-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_ltoa.c
56 lines (51 loc) · 1.44 KB
/
ft_ltoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/07 09:39:00 by rbetz #+# #+# */
/* Updated: 2022/10/31 10:51:02 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static long ft_countdigits(long nbr)
{
long n;
n = 0;
if (nbr == 0)
return (1);
while (nbr != 0)
{
nbr = nbr / 10;
n++;
}
return (n);
}
char *ft_ltoa(long nbr)
{
char *str;
long i;
long sign;
i = ft_countdigits(nbr);
sign = 0;
if (nbr < 0)
sign = 1;
str = malloc(i + sign + 1);
if (str == NULL)
return (NULL);
str[i + sign] = '\0';
if (sign == 1)
str[0] = '-';
while (i > 0)
{
if (sign == 1)
str[i + sign - 1] = (nbr % 10) * -1 + '0';
else
str[i + sign - 1] = nbr % 10 + '0';
nbr = nbr / 10;
i--;
}
return (str);
}