-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
55 lines (50 loc) · 1.33 KB
/
ft_itoa.c
File metadata and controls
55 lines (50 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cmachado <cmachado@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/01 19:26:29 by cmachado #+# #+# */
/* Updated: 2022/03/05 16:38:30 by cmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int num_dig(int n)
{
int dig;
dig = 0;
while (n != 0)
{
n /= 10;
dig++;
}
return (dig);
}
char *ft_itoa(int n)
{
int neg;
char *new;
int i;
neg = 1;
i = num_dig(n);
if (n <= 0)
{
neg = -1;
i++;
}
new = (char *) malloc(i + 1);
if (!new)
return (NULL);
if (n == 0)
new[0] = '0';
else if (n < 0)
new[0] = '-';
new[i--] = '\0';
while (n != 0)
{
new[i--] = neg * (n % 10) + 48;
n /= 10;
}
return (new);
}