-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
74 lines (68 loc) · 1.63 KB
/
Copy pathft_itoa.c
File metadata and controls
74 lines (68 loc) · 1.63 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asuc <asuc@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/25 23:28:48 by asuc #+# #+# */
/* Updated: 2023/11/09 02:18:11 by asuc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_numlen(long long int n)
{
int len;
len = 0;
if (n == 0)
return (1);
if (n < 0)
{
n = -n;
len++;
}
while (n)
{
n /= 10;
len++;
}
return (len);
}
static void neg_nbr(unsigned int n, int len, char **str)
{
(*str) = ft_calloc((len + 1), sizeof(char));
if ((*str) == NULL)
return ;
(*str)[0] = '-';
len--;
while (len > 0)
{
(*str)[len] = n % 10 + '0';
n /= 10;
len--;
}
}
char *ft_itoa(int n)
{
int len;
char *str;
if (!n)
return (ft_strdup("0"));
len = ft_numlen(n);
if (n < 0)
{
neg_nbr(-n, len, &str);
return (str);
}
str = ft_calloc(sizeof(char), (len + 1));
if (str == NULL)
return (NULL);
len--;
while (len >= 0)
{
str[len] = n % 10 + '0';
n /= 10;
len--;
}
return (str);
}