-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
59 lines (54 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nvienot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/15 22:18:46 by nvienot #+# #+# */
/* Updated: 2019/03/16 02:19:45 by nvienot ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int ft_int_len(int n)
{
int i;
i = 0;
if (n < 0)
{
i++;
n = -n;
}
while (n > 0)
{
n = (n - (n % 10)) / 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
int len;
char *repitoa;
len = ft_int_len(n);
if (n == -2147483648)
return (ft_strdup("-2147483648"));
if (n == 0)
return (ft_strdup("0"));
if (!(repitoa = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
if (n < 0)
{
repitoa[0] = '-';
n = -n;
}
repitoa[len] = '\0';
while (n > 0)
{
repitoa[len - 1] = n % 10 + '0';
n = (n - (n % 10)) / 10;
len--;
}
return (repitoa);
}