-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
59 lines (54 loc) · 1.45 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
59
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gyong-si <gyongsi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/20 13:02:16 by gyong-si #+# #+# */
/* Updated: 2023/09/22 16:48:45 by gyong-si ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <limits.h>
#include <stdio.h>
size_t ft_nbrlen(int n)
{
size_t i;
i = 0;
if (n <= 0)
i = 1;
while (n)
{
n /= 10;
++i;
}
return (i);
}
char *ft_itoa(int n)
{
char *result;
int len;
if (n == INT_MIN)
return (ft_strdup("-2147483648"));
len = ft_nbrlen(n);
result = ft_calloc(len + 1, sizeof(char));
if (!result)
return (NULL);
if (n == 0)
result[0] = '0';
if (n < 0)
{
result[0] = '-';
n = -n;
}
result[len] = '\0';
len--;
while (len >= 0 && n != 0)
{
result[len] = n % 10 + '0';
len--;
n /= 10;
}
return (result);
}