-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
61 lines (56 loc) · 1.65 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: chrhuang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/08 11:07:01 by chrhuang #+# #+# */
/* Updated: 2018/11/12 13:42:30 by chrhuang ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *check(int n, int neg)
{
if (n == 0)
return (ft_strdup("0"));
else
return (neg == 0 ? ft_strdup("1") : ft_strdup("-1"));
}
static void count(long *tmp, int *i, int n)
{
*tmp = 1;
*i = 0;
while (*tmp <= n)
{
*tmp = *tmp * 10;
*i = *i + 1;
}
}
char *ft_itoa(int n)
{
char *str;
int neg;
int i;
long tmp;
neg = n < 0 ? 1 : 0;
n = n < 0 ? -n : n;
if (n == -2147483648)
return (ft_strjoin(ft_itoa(-21474), ft_itoa(83648)));
if (n == 0 || n == 1)
return (check(n, neg));
count(&tmp, &i, n);
if ((str = ft_strnew(i + neg)) == NULL)
return (NULL);
neg == 1 ? str[0] = '-' : 0;
tmp = tmp / 10;
while (tmp != 0)
{
str[neg] = n / (tmp) + '0';
n = n % tmp;
tmp = tmp / 10;
neg = neg + 1;
}
str[neg] = '\0';
return (str);
}