-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
55 lines (50 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edebi <edebi@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/02 01:17:09 by edebi #+# #+# */
/* Updated: 2020/11/04 15:47:32 by edebi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_len(int n)
{
int len;
if (n == 0)
return (1);
len = 0;
if (n < 0)
len++;
while (n)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
int len;
char *str;
unsigned int nbr;
nbr = (n > 0) ? n : -n;
len = count_len(n);
if (!(str = (char *)malloc(sizeof(char) * len + 1)))
return (NULL);
*(str + len--) = '\0';
while (nbr > 0)
{
*(str + len--) = nbr % 10 + '0';
nbr /= 10;
}
if (n < 0)
{
*str = '-';
}
if (n == 0)
*str = '0';
return (str);
}