-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
65 lines (58 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jmilson- <jmilson-@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/16 01:53:23 by jmilson- #+# #+# */
/* Updated: 2021/09/23 17:34:30 by jmilson- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_counter(int n)
{
size_t i;
i = 0;
if (n <= 0)
i++;
while (n)
{
i++;
n = n / 10;
}
return (i);
}
static char *ft_brain(char *s, size_t i, int n)
{
if (n == 0)
s[0] = '0';
if (n < 0)
{
s[0] = '-';
n = -n;
}
i = i - 1;
if (n > 9)
{
while (n != 0)
{
s[i] = (n % 10) + 48;
n = n / 10;
i--;
}
}
else
s[i] = n + 48;
return (s);
}
char *ft_itoa(int n)
{
char *str;
size_t len;
len = ft_counter(n);
str = (char *)ft_calloc(len + 1, sizeof(char));
if (str == NULL)
return (NULL);
return (ft_brain(str, len, n));
}