-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
61 lines (56 loc) · 1.43 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: pmalope <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/18 12:11:11 by pmalope #+# #+# */
/* Updated: 2019/06/18 13:36:26 by pmalope ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_int_len(int n)
{
int len;
len = 0;
if (n == 0)
len++;
if (n < 0)
{
n *= -1;
len++;
}
while (n)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
int len;
char *str;
len = ft_int_len(n);
str = (char *)malloc(sizeof(char) * len + 1);
if (!str)
return (NULL);
if (n == -2147483648)
ft_strcpy(str, "-2147483648");
str[len] = '\0';
if (n == 0)
str[0] = '0';
if (n < 0)
{
str[0] = '-';
n *= -1;
}
while (n > 0)
{
str[len - 1] = '0' + (n % 10);
n = n / 10;
len--;
}
return (str);
}