-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
58 lines (53 loc) · 1.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kokaimov <kokaimov@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:30:56 by kokaimov #+# #+# */
/* Updated: 2023/11/17 16:31:57 by kokaimov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int int_len(int n)
{
int len;
if (n <= 0)
len = 1;
else
len = 0;
while (n != 0)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
long num;
int len;
char *str;
num = n;
len = int_len(num);
str = ft_calloc(len + 1, sizeof(char));
if (!str)
return (NULL);
if (num < 0)
{
str[0] = '-';
num = -num;
}
else if (num == 0)
{
str[0] = '0';
return (str);
}
while (num != 0)
{
str[--len] = (num % 10) + '0';
num /= 10;
}
return (str);
}