-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
62 lines (56 loc) · 1.42 KB
/
ft_itoa.c
File metadata and controls
62 lines (56 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_itoa.c :+: :+: */
/* +:+ */
/* By: jsmidt <jsmidt@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/10/21 14:13:16 by jsmidt #+# #+# */
/* Updated: 2025/10/23 16:56:18 by jsmidt ######## odam.nl */
/* */
/* ************************************************************************** */
// #include <limits.h>
#include "libft.h"
static int ilen(long n)
{
int i;
i = 1;
while (n / 10)
{
n = n / 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
long m;
char *res;
int i;
int neg;
m = n;
neg = 0;
if (m < 0)
{
m = m * -1;
neg = 1;
}
i = ilen(m) + neg;
res = malloc((i * sizeof(char) + 1));
if (!res)
return (NULL);
res[i] = '\0';
if (neg)
res[0] = '-';
while (i - neg)
{
res[--i] = (m % 10) + '0';
m = m / 10;
}
return (res);
}
// int main(void)
// {
// int n = INT_MAX;
// printf("%s\n", ft_itoa(n));
// }