-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
64 lines (59 loc) · 1.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: achraiti <achraiti@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 14:59:40 by achraiti #+# #+# */
/* Updated: 2023/12/17 11:55:15 by achraiti ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int len_of_n(int n)
{
int i;
i = 0;
if (n == 0)
{
return (1);
}
if (n < 0)
{
n = -n;
i++;
}
while (n > 0)
{
n = n / 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
int len;
char *m;
if (n == MIN_INT)
return (ft_strdup("-2147483648"));
if (n == 0)
return (ft_strdup("0"));
len = len_of_n(n);
m = (char *)malloc(sizeof(char) * (len + 1));
if (!m)
return (0);
if (n < 0)
{
n = -n;
m[0] = '-';
}
m[len] = '\0';
len--;
while (n > 0)
{
m[len] = (n % 10) + '0';
n = n / 10;
len--;
}
return (m);
}