-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
72 lines (66 loc) · 1.62 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pablomar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/13 16:33:04 by pablomar #+# #+# */
/* Updated: 2019/11/13 23:53:50 by pablomar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static int ft_len(long int num)
{
int len;
len = 0;
if (num == 0)
len = 1;
if (num < 0)
{
num = -1 * num;
len++;
}
while (num > 0)
{
num = num / 10;
len++;
}
return (len);
}
static char *algo(long int num, int len, char *itoa, int i)
{
while (num > 0)
{
itoa[(len--) - 1] = (num % 10) + '0';
num = num / 10;
i++;
}
return (itoa);
}
char *ft_itoa(int n)
{
int len;
char *itoa;
int i;
long int num;
num = n;
len = ft_len(num);
i = 0;
if (!(itoa = (void*)malloc(sizeof(char) * (len + 1))))
return (NULL);
itoa[len] = '\0';
if (num == 0)
{
itoa[0] = 48;
return (itoa);
}
if (num < 0)
{
itoa[0] = '-';
num = num * -1;
}
algo(num, len, itoa, i);
return (itoa);
}