-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
57 lines (52 loc) · 1.43 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: yeonhkim <yeonhkim@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/21 18:35:48 by yeonhkim #+# #+# */
/* Updated: 2022/07/21 18:55:46 by yeonhkim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t get_length(int n)
{
size_t len;
len = 0;
if (n <= 0)
len++;
while (n)
{
len++;
n /= 10;
}
return (len);
}
char *ft_itoa(int n)
{
char *dst;
char *cur;
size_t len;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
else if (n == 0)
return (ft_strdup("0"));
len = get_length(n);
dst = malloc(sizeof(char) * (len + 1));
if (!dst)
return (NULL);
ft_bzero(dst, len + 1);
if (n < 0)
{
*dst = '-';
n = -n;
}
cur = dst + len - 1;
while (n)
{
*cur-- = (n % 10) + '0';
n /= 10;
}
return (dst);
}