-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
56 lines (51 loc) · 1.48 KB
/
ft_itoa.c
File metadata and controls
56 lines (51 loc) · 1.48 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/17 17:14:43 by jbadaire #+# #+# */
/* Updated: 2023/01/17 17:14:59 by jbadaire ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_size(long int number)
{
int index;
if (number == 0)
return (1);
index = 0;
while (number)
{
number = number / 10;
index++;
}
return (index);
}
char *ft_itoa(int n)
{
long nb;
int is_transformed;
int mlc_size;
char *mlc;
nb = n;
is_transformed = 0;
if (n < 0)
{
nb = (long) n * -1;
is_transformed = 1;
}
mlc_size = ft_count_size(nb) + is_transformed;
mlc = ft_calloc(mlc_size + 1, sizeof(char));
if (!mlc)
return (0);
while (mlc_size)
{
mlc[--mlc_size] = nb % 10 + '0';
nb = nb / 10;
}
if (n < 0)
mlc[mlc_size] = '-';
return (mlc);
}