-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
60 lines (55 loc) · 1.45 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: erli <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/08 14:19:28 by erli #+# #+# */
/* Updated: 2018/11/09 12:00:56 by erli ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
static int max_power(int nb, int *pow)
{
int n_pow;
n_pow = 0;
if (nb < 0)
*pow = -1;
if (nb == -2147483648)
{
*pow *= 10;
n_pow++;
}
while (nb / *pow >= 10)
{
*pow *= 10;
n_pow++;
}
return (n_pow);
}
char *ft_itoa(int nb)
{
int pow;
int i;
char *str;
pow = 1;
i = 0;
if (!(str = (char *)malloc(sizeof(char) * (max_power(nb, &pow) + 2))))
return (NULL);
if (nb < 0)
{
str[0] = '-';
i++;
}
while (pow != 0)
{
str[i] = nb / pow + 48;
nb = nb % pow;
pow /= 10;
i++;
}
str[i] = '\0';
return (str);
}