-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
59 lines (54 loc) · 1.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abdsalah <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/29 15:02:25 by abdsalah #+# #+# */
/* Updated: 2024/08/31 13:25:13 by abdsalah ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int len(int n)
{
int len;
if (n == 0)
return (1);
len = 0;
if (n < 0)
len++;
while (n != 0)
{
len++;
n /= 10;
}
return (len);
}
char *ft_itoa(int n)
{
int numlen;
char *str;
int is_negative;
unsigned int num;
is_negative = 0;
num = n;
if (n < 0)
{
is_negative = 1;
num = -n;
}
numlen = len(n);
str = malloc(numlen + 1);
if (!str)
return (NULL);
str[numlen] = '\0';
if (is_negative)
str[0] = '-';
while (numlen > is_negative)
{
str[--numlen] = (num % 10) + '0';
num /= 10;
}
return (str);
}