-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
82 lines (73 loc) · 1.73 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
81
82
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sizerese <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/30 17:29:10 by sizerese #+# #+# */
/* Updated: 2023/08/11 19:55:13 by sizerese ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count(int num)
{
int i;
i = 0;
if (num == 0)
return (1);
if (num < 0)
{
num *= -1;
i++;
}
while (num > 0)
{
num /= 10;
i++;
}
return (i);
}
static char *ft_fill_numbers(char *buffer, int num, long int i)
{
int j;
j = 1;
if (num < 0)
{
num *= -1;
buffer[0] = '-';
}
if (num == 0)
buffer[0] = '0';
while (num > 0)
{
buffer[i - j] = num % 10 + 48;
num /= 10;
j++;
}
buffer[i] = '\0';
return (buffer);
}
char *ft_itoa(int num)
{
long int i;
char *buffer;
i = ft_count(num);
if (num == -2147483648)
{
buffer = ft_strdup("-2147483648");
return (buffer);
}
buffer = (char *) malloc(sizeof(char) * (i + 1));
if (!buffer)
return (NULL);
buffer = ft_fill_numbers(buffer, num, i);
return (buffer);
}
/*
int main(void)
{
int num = 1222223434534545435;
printf("%s\n", ft_itoa(num));
}
*/