-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
70 lines (63 loc) · 1.53 KB
/
ft_itoa.c
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
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbenchel <mbenchel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 22:22:40 by mbenchel #+# #+# */
/* Updated: 2023/11/13 03:55:58 by mbenchel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t intlen(long long n)
{
int i;
i = 0;
if (n == 0)
return (1);
if (n < 0)
{
n *= -1;
}
while (n > 0)
{
n = n / 10;
i++;
}
return (i);
}
static char *itoap(int len, long long n, char *str)
{
int neg;
neg = 0;
if (n < 0)
{
n *= -1;
neg = 1;
str[0] = '-';
}
str[len] = '\0';
while (len > neg)
{
str[len - 1] = (n % 10) + 48;
n = n / 10;
len--;
}
return (str);
}
char *ft_itoa(int n)
{
char *res;
long long nb;
int len;
len = intlen(n);
nb = n;
if (n < 0)
len = len + 1;
res = (char *)malloc((len + 1) * sizeof(char));
if (res == NULL)
return (NULL);
itoap(len, n, res);
return (res);
}