-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
61 lines (55 loc) · 1.46 KB
/
ft_itoa.c
File metadata and controls
61 lines (55 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 12:50:39 by dporhomo #+# #+# */
/* Updated: 2025/11/24 10:07:16 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_get_size(long n);
char *ft_itoa(int n)
{
char *str;
long num;
size_t size;
num = n;
size = ft_get_size(num);
str = (char *)malloc(sizeof(char) * (size + 1));
if (!str)
return (NULL);
str[size] = '\0';
if (num == 0)
str[0] = '0';
if (num < 0)
num *= -1;
while (num > 0)
{
str[--size] = (num % 10) + '0';
num /= 10;
}
if (n < 0)
str[0] = '-';
return (str);
}
static int ft_get_size(long n)
{
int size;
if (n == 0)
return (1);
size = 0;
if (n < 0)
{
size++;
n *= -1;
}
while (n > 0)
{
n /= 10;
size++;
}
return (size);
}