-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
42 lines (39 loc) · 1.25 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: schetty <schetty@student.42kl.edu.my> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/10 13:46:03 by schetty #+# #+# */
/* Updated: 2021/11/12 11:55:29 by schetty ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
int len;
long nb;
char *str;
len = ft_intlen_base(n, 10);
nb = n;
if (nb < 0)
{
nb = -nb;
len += 1;
}
str = malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
str[len] = '\0';
if (nb == 0)
str[--len] = '0';
while (nb)
{
str[--len] = (nb % 10) + '0';
nb /= 10;
}
if (len)
str[0] = '-';
return (str);
}