-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
57 lines (51 loc) · 1.46 KB
/
ft_itoa.c
File metadata and controls
57 lines (51 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aabduvak <aabduvak@42ISTANBUL.COM.TR> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/03 12:30:11 by aabduvak #+# #+# */
/* Updated: 2022/01/07 17:27:09 by aabduvak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_abs(int nb)
{
if (nb < 0)
nb *= -1;
return (nb);
}
static int ft_count_leng(int num)
{
int i;
i = 0;
while (num > 9 || num < -9)
{
num = num / 10;
i++;
}
i++;
return (i);
}
char *ft_itoa(int value)
{
char *ret;
int leng;
int is_negative;
is_negative = value < 0;
leng = ft_count_leng(value) + is_negative;
ret = (char *) malloc(sizeof(char) * (leng + 1));
if (!ret)
return (NULL);
ret[leng--] = '\0';
while (leng >= 0)
{
ret[leng] = ft_abs(value % 10) + '0';
value /= 10;
leng--;
}
if (is_negative)
ret[0] = '-';
return (ret);
}