-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
57 lines (52 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sonyacorcoran <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/17 16:06:03 by sonyacorcor #+# #+# */
/* Updated: 2023/08/24 11:51:37 by sonyacorcor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_len(int n)
{
int len;
len = 0;
if (n <= 0)
len = 1;
while (n)
{
len++;
n = n / 10;
}
return (len);
}
char *ft_itoa(int n)
{
char *num;
int len;
unsigned int copy;
len = ft_len(n);
num = (char *)malloc(sizeof(char) * (len + 1));
if (!num)
return (NULL);
num[len--] = '\0';
if (n == 0)
num[0] = '0';
if (n < 0)
{
copy = n * -1;
num[0] = '-';
}
else
copy = n;
while (copy)
{
num[len] = 48 + (copy % 10);
copy = copy / 10;
len--;
}
return (num);
}