-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
52 lines (47 loc) · 1.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sbin-jef <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/28 15:16:57 by sbin-jef #+# #+# */
/* Updated: 2024/06/29 23:33:47 by sbin-jef ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static size_t get_num_len(int n)
{
size_t len;
len = (n <= 0);
while (n)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
char *str;
size_t len;
unsigned int num;
len = get_num_len(n);
str = (char *)malloc(len + 1);
if (!str)
return (NULL);
str[len] = '\0';
if (n < 0)
num = -n;
else
num = n;
while (len--)
{
str[len] = num % 10 + '0';
num /= 10;
}
if (n < 0)
str[0] = '-';
return (str);
}