-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
49 lines (45 loc) · 1.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hnabil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/24 22:43:05 by hnabil #+# #+# */
/* Updated: 2019/10/29 21:56:13 by hnabil ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_size(int n)
{
if (n >= 0 && n <= 9)
return (1);
if (n >= -9 && n < 0)
return (2);
else
return (1 + ft_size(n / 10));
}
char *ft_itoa(int n)
{
char *str;
int len;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
len = ft_size(n);
if (!(str = (char *)malloc(len + 1)))
return (0);
str[len] = '\0';
if (n < 0)
{
str[0] = '-';
n *= -1;
}
if (!n)
str[0] = '0';
while (n)
{
str[--len] = (n % 10) + '0';
n /= 10;
}
return (str);
}