-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
61 lines (55 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungsle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 20:28:15 by kyungsle #+# #+# */
/* Updated: 2021/12/05 12:33:06 by kyungsle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void *ft_set_result(int n, char *result, int *i)
{
if (n >= 10)
{
ft_set_result(n / 10, result, i);
}
result[(*i)++] = (n % 10) + '0';
return (0);
}
static int ft_get_size(int n)
{
int result;
result = 0;
if (n < 0)
n *= -1;
while (n > 0)
{
n /= 10;
result++;
}
return (result + 1);
}
char *ft_itoa(int n)
{
int i;
char *result;
i = 0;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
if (n == 0)
return (ft_strdup("0"));
result = malloc(sizeof(char) * ft_get_size(n) + (n < 0));
if (!result)
return (NULL);
if (n < 0)
{
result[i++] = '-';
n *= -1;
}
ft_set_result(n, result, &i);
result[i] = '\0';
return (result);
}