-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
60 lines (55 loc) · 1.44 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vsimeono <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/19 19:14:17 by vsimeono #+# #+# */
/* Updated: 2021/10/02 20:37:13 by vsimeono ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_int_lenght(int n)
{
int i;
i = 1;
if (n < 0)
{
n *= -1;
i++;
}
while (n >= 10)
{
n /= 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
char *temp;
char *str;
size_t len;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
len = ft_count_int_lenght(n);
temp = ft_calloc(sizeof(char), (len + 1));
if (!temp)
return (NULL);
str = temp;
if (n < 0)
{
*temp = '-';
n *= -1;
}
temp += len;
while (n >= 10)
{
temp--;
*temp = ((n % 10) + '0');
n /= 10;
}
*(--temp) = (n % 10) + '0';
return (str);
}