-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
executable file
·55 lines (51 loc) · 1.36 KB
/
ft_itoa.c
File metadata and controls
executable file
·55 lines (51 loc) · 1.36 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: kalshaer <kalshaer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 08:35:35 by kalshaer #+# #+# */
/* Updated: 2023/01/13 07:54:49 by kalshaer ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void toput(char *r, long int nb, long int d)
{
if (nb == 0)
r[0] = '0';
if (nb < 0)
{
r[0] = '-';
nb = nb * -1;
}
while (nb > 0)
{
r[d - 1] = (nb % 10) + '0';
nb = nb / 10;
d--;
}
}
char *ft_itoa(int n)
{
char *r;
long int d;
long int i;
long int nm;
d = 0;
i = n;
nm = n;
while (i != 0)
{
i = i / 10;
d++;
}
if (n <= 0)
d++;
r = ft_calloc((d + 1), sizeof(char));
if (!r)
return (0);
toput(r, nm, d);
r[d] = 0;
return (r);
}