-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
63 lines (57 loc) · 1.54 KB
/
ft_itoa.c
File metadata and controls
63 lines (57 loc) · 1.54 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
57
58
59
60
61
62
63
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gaducurt <gaducurt@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/22 08:43:52 by gaducurt #+# #+# */
/* Updated: 2026/01/14 16:18:33 by gaducurt ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count(int n)
{
int i;
i = 0;
if (n < 0)
n = -n;
while (n != 0)
{
n /= 10;
i++;
}
return (i);
}
static char *ft_putdest(size_t count, int n, char *dest, size_t i)
{
while (count-- > i)
{
dest[count] = (n % 10) + '0';
n /= 10;
}
return (dest);
}
char *ft_itoa(int n)
{
char *dest;
size_t count;
size_t i;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
count = ft_count(n);
i = 0;
if (n < 0 || count == 0)
count++;
dest = ft_calloc(sizeof(char), count + 1);
if (!(dest))
return (NULL);
else if (n < 0)
{
n = -n;
dest[0] = '-';
i++;
}
dest = ft_putdest(count, n, dest, i);
return (dest);
}