-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
55 lines (50 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: togauthi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/11 07:51:28 by togauthi #+# #+# */
/* Updated: 2024/10/17 10:10:06 by togauthi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int alloc_size(int n)
{
int size;
size = 0;
while (n != 0)
{
n /= 10;
size++;
}
return (size);
}
char *ft_itoa(int n)
{
char *str;
int size;
int neg;
long nbr;
nbr = (long)n;
size = alloc_size(nbr);
if (nbr < 0 || size == 0)
size++;
neg = 0;
str = ft_calloc(size + 1, sizeof(char));
if (!str)
return (NULL);
if (nbr < 0)
{
neg++;
str[0] = '-';
nbr = nbr * -1;
}
while (size-- > neg)
{
str[size] = (nbr % 10) + '0';
nbr = nbr / 10;
}
return (str);
}