-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
53 lines (48 loc) · 1.36 KB
/
ft_itoa.c
File metadata and controls
53 lines (48 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_itoa.c :+: :+: */
/* +:+ */
/* By: rcappend <rcappend@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/11/02 11:36:52 by rcappend #+# #+# */
/* Updated: 2021/11/03 14:23:37 by rcappend ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static int counter(int n)
{
int i;
i = 1;
if (n < 0)
i++;
while (n / 10 != 0)
{
i++;
n = n / 10;
}
return (i);
}
char *ft_itoa(int n)
{
char *ret;
int len;
int minus;
minus = 0;
if (n < 0)
minus = 1;
len = counter(n);
ret = malloc(sizeof(unsigned char) * len + 1);
if (!ret)
return (NULL);
ret[len] = '\0';
while (len > minus)
{
len--;
ret[len] = ft_abs((n % 10)) + '0';
n = n / 10;
}
if (minus)
ret[len - 1] = '-';
return (ret);
}