-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
67 lines (60 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpicoli- < lpicoli-@student.42porto.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/15 09:52:59 by lpicoli- #+# #+# */
/* Updated: 2022/11/23 07:35:05 by lpicoli- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_digits(long n)
{
int digits;
digits = 0;
if (n <= 0)
{
n *= -1;
digits++;
}
while (n > 0)
{
n /= 10;
digits++;
}
return (digits);
}
char *ft_itoa(int n)
{
size_t i;
char *str;
i = ft_count_digits(n);
str = (char *)malloc((i + 1) * sizeof(char));
if (!str)
return (NULL);
str[i--] = '\0';
if (n == -2147483648)
str = "-2147483648";
else if (n < 0)
{
str[0] = '-';
n *= -1;
}
else if (n == 0)
str[0] = '0';
while (n > 0)
{
str[i] = 48 + (n % 10);
i--;
n /= 10;
}
return (str);
}
/*int main()
{
int n;
n = -2147483648;
printf("Decimal number: %d, string convertion: %s\n", n, ft_itoa(n));
}*/