-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
58 lines (53 loc) · 1.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aerrajiy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/09 22:20:09 by aerrajiy #+# #+# */
/* Updated: 2022/10/17 17:32:27 by aerrajiy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int return_len(long n)
{
int i;
i = 0;
if (n < 0)
i++;
if (n == 0)
return (1);
while (n)
{
n /= 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
int len;
char *tab;
long new;
new = n;
len = return_len(new);
tab = (char *)malloc(sizeof(char) * (len + 1));
if (tab != NULL)
{
tab[len--] = '\0';
if (new == 0)
tab[0] = 48;
if (new < 0)
{
tab[0] = '-';
new *= -1;
}
while (new)
{
tab[len--] = new % 10 + 48;
new /= 10;
}
}
return (tab);
}