-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
73 lines (66 loc) · 1.6 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lwilliam <lwilliam@student.42kl.edu.my> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/30 17:14:10 by lwilliam #+# #+# */
/* Updated: 2022/06/06 15:03:56 by lwilliam ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count(int c)
{
int x;
x = 0;
if (c == -2147483648)
return (11);
if (c == 0)
return (1);
if (c < 0)
{
c = c * -1;
x++;
}
while (c != 0)
{
c = c / 10;
x++;
}
return (x);
}
static int filter(int i)
{
if (i == -2147483648)
i = 147483648;
else if (i < 0)
i *= -1;
return (i);
}
char *ft_itoa(int n)
{
char *num;
int len;
len = count(n);
num = malloc(sizeof(char) * (len + 1));
if (!(num))
return (NULL);
num[len--] = '\0';
if (n == 0)
num[0] = '0';
if (n < 0)
num[0] = '-';
if (n == -2147483648)
num[1] = '2';
while (n != 0)
{
num[len--] = ((filter(n) % 10) + 48);
n = n / 10;
}
return (num);
}
// int main(void)
// {
// printf("%s\n", ft_itoa(-2147483648));
// }