-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
60 lines (55 loc) · 1.44 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
60
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yoaoki <yoaoki@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 01:01:08 by yoaoki #+# #+# */
/* Updated: 2024/05/01 22:12:34 by yoaoki ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_numlen(long long n)
{
size_t len;
if (n == 0)
return (1);
len = 0;
if (n < 0)
{
len++;
n *= -1;
}
while (n > 0)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
long long num;
char *result;
size_t len;
num = n;
len = ft_numlen(num);
result = (char *)malloc(sizeof(char) * (len + 1));
if (!result)
return (0);
result[len--] = '\0';
if (num == 0)
*result = '0';
if (num < 0)
{
num *= -1;
*result = '-';
}
while (num > 0)
{
result[len--] = (num % 10) + '0';
num /= 10;
}
return (result);
}