-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
79 lines (73 loc) · 1.92 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alefebvr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/14 11:42:05 by alefebvr #+# #+# */
/* Updated: 2017/11/19 22:08:17 by alefebvr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isnegative(int nb)
{
if (nb < 0)
return (1);
else
return (0);
}
static int ft_combiendechiffres(int nb)
{
int chiffres;
int a;
a = 0;
chiffres = 0;
if (nb < 0 && nb != -2147483648)
{
nb = -nb;
chiffres++;
}
if (nb == -2147483648)
{
nb = -1 - nb;
chiffres++;
}
while (nb > 9)
{
nb = nb / 10;
if (chiffres == 1 && nb == (2147483647 / 10) && a != 1)
nb++;
a = 1;
chiffres++;
}
chiffres++;
return (chiffres);
}
char *ft_itoa(int n)
{
int negative;
int chiffres;
char *fraiche;
negative = ft_isnegative(n);
chiffres = ft_combiendechiffres(n);
if (negative == 1 && n != -2147483648)
n = -n;
if (n == -2147483648)
n = -1 - n;
if ((fraiche = malloc(sizeof(char) * chiffres + 1)) == NULL)
return (NULL);
if (negative == 1)
fraiche[0] = '-';
fraiche[chiffres] = '\0';
while (n > 9)
{
if (negative == 1 && n == 2147483647)
fraiche[--chiffres] = n % 10 + 1 + '0';
else
fraiche[--chiffres] = n % 10 + '0';
n = n / 10;
}
fraiche[--chiffres] = n + '0';
return (fraiche);
}