-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
53 lines (48 loc) · 1.36 KB
/
ft_itoa.c
File metadata and controls
53 lines (48 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: omaly <omaly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/03 15:29:48 by omaly #+# #+# */
/* Updated: 2025/06/03 16:07:11 by omaly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_intlen(int num)
{
int len;
len = (num <= 0);
while (num)
{
len++;
num = num / 10;
}
return (len);
}
char *ft_itoa(int num)
{
int len;
long nb;
char *str;
len = ft_intlen(num);
nb = num;
str = (char *)malloc(sizeof(char) * (len + 1));
if (len == 0)
return (NULL);
str[len] = '\0';
if (nb == 0)
str[0] = '0';
if (nb < 0)
{
str[0] = '-';
nb = -nb;
}
while (nb)
{
str[--len] = (nb % 10) + '0';
nb = nb / 10;
}
return (str);
}