-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
53 lines (49 loc) · 1.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wbertoni <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/27 18:35:31 by wbertoni #+# #+# */
/* Updated: 2020/01/28 18:16:16 by wbertoni ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *createstr(char *a, int n, int sign, int size)
{
while (size >= 0)
{
if (sign < 0 && size == 0)
return (a);
a[size] = (n % 10) + 48;
n /= 10;
size--;
}
return (a);
}
char *ft_itoa(int n)
{
int nbr;
int sign;
int size;
char *a;
size = 0;
sign = 1;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
if (n < 0)
sign = -1;
nbr = n * sign;
while (nbr /= 10)
size++;
(sign < 0) ? size++ : size;
a = ft_newstr(size + 1);
if (a == NULL)
return (NULL);
if (sign < 0)
a[0] = '-';
a[ft_strlen(a)] = '\0';
n *= sign;
return (createstr(a, n, sign, size));
}