-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
62 lines (56 loc) · 1.41 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zbabahmi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/31 05:09:05 by zbabahmi #+# #+# */
/* Updated: 2022/10/31 05:09:06 by zbabahmi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
long int ft_counter(int c)
{
int i;
i = 0;
if (c <= 0)
i++;
while (c != 0)
{
i++;
c /= 10;
}
return (i);
}
void converter(char *p, long int i, long int a)
{
while (a != 0)
{
p[i] = (a % 10) + 48;
i--;
a /= 10;
}
}
char *ft_itoa(int n)
{
long int i;
char *ptr;
long int b;
b = (long int)n;
i = ft_counter(b);
ptr = malloc(i + 1);
if (!ptr)
return (0);
ptr[i] = '\0';
i--;
if (b == 0)
ptr[0] = '0';
else if (b < 0)
{
b *= (-1);
ptr[0] = '-';
}
converter(ptr, i, b);
return (ptr);
}