-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
55 lines (50 loc) · 1.39 KB
/
ft_itoa.c
File metadata and controls
55 lines (50 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atutuncu <atutuncu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/02 13:41:43 by atutuncu #+# #+# */
/* Updated: 2023/01/02 14:20:35 by atutuncu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_numlen(int nb)
{
int len;
len = 0;
if (nb < 0)
len++;
while (nb)
{
nb = nb / 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
int len;
char *ret;
const char *dig;
dig = "0123456789";
len = ft_numlen(n);
if (n == 0)
return (ft_strdup("0"));
ret = malloc(sizeof(char) * (len + 1));
if (!ret)
return (0);
ret[len] = 0;
if (n < 0)
ret[0] = '-';
while (n)
{
if (n > 0)
ret[--len] = dig[n % 10];
else
ret[--len] = dig[n % 10 * -1];
n /= 10;
}
return (ret);
}