-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
73 lines (67 loc) · 1.58 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: flolefeb <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 17:01:16 by flolefeb #+# #+# */
/* Updated: 2019/11/26 14:37:42 by flolefeb ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_len(long int n)
{
int len;
int a;
len = 0;
a = 0;
if (n == 0)
return (1);
if (n < 0)
{
n = -n;
a = 1;
}
while (n > 0)
{
n = n / 10;
len++;
}
if (a == 1)
len++;
return (len);
}
static char *ft_fill(int i, int a, char *str, long int nb)
{
while (i >= a)
{
str[i--] = (nb % 10) + 48;
nb = nb / 10;
}
return (str);
}
char *ft_itoa(int n)
{
char *str;
int i;
long int nb;
int a;
int len;
nb = n;
i = 0;
a = 0;
len = ft_len(nb);
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
if (nb < 0)
{
str[0] = '-';
nb = -nb;
a = 1;
}
i = (len) - 1;
str = ft_fill(i, a, str, nb);
str[len] = '\0';
return (str);
}