-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
76 lines (67 loc) · 1.71 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
74
75
76
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iziane <iziane@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/14 02:04:18 by iziane #+# #+# */
/* Updated: 2024/03/15 16:49:09 by iziane ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int buffsize(long cpy)
{
int counter;
if (cpy < 0)
cpy = -cpy;
counter = 1;
while (cpy > 9)
{
cpy = cpy / 10;
counter++;
}
return (counter);
}
static char *filler(int sign, int buffer, long n)
{
char *new;
int i;
new = (char *)malloc(sizeof(char) * (buffer + 1));
if (new == NULL)
return (NULL);
i = buffer - 1;
if (sign == 1)
new[0] = '-';
while (i >= sign)
{
new[i] = (n % 10) + '0';
n = n / 10;
i--;
}
new[buffer] = '\0';
return (new);
}
char *ft_itoa(int n)
{
int sign;
int buffer;
long xcase;
sign = 0;
xcase = n;
buffer = buffsize(xcase);
if (xcase < 0)
{
buffer = buffer + 1;
sign = 1;
xcase = (-1) * xcase;
}
return (filler(sign, buffer, xcase));
}
// int main(void)
// {
// int n;
// n = -2147483648;
// printf("Itoa: %s\n", ft_itoa(n));
// return (0);
// }