-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
54 lines (49 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tairribe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/11 18:17:38 by tairribe #+# #+# */
/* Updated: 2022/06/19 15:28:31 by tairribe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_size(int n)
{
int i;
i = 1;
while (n > 9 || n < -9)
{
i++;
n = n / 10;
}
if (n < 0)
i++;
return (i);
}
char *ft_itoa(int n)
{
int size;
char *str_nb;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
if (n == 0)
return (ft_strdup("0"));
size = get_size(n);
str_nb = ft_calloc(size + 1, sizeof(char));
if (n < 0)
{
str_nb[0] = '-';
n = n * -1;
}
size--;
while (n)
{
str_nb[size] = (n % 10) + '0';
n = n / 10;
size--;
}
return (str_nb);
}