-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
72 lines (65 loc) · 1.79 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
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkahvedj <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/30 23:19:26 by jkahvedj #+# #+# */
/* Updated: 2021/06/12 20:06:29 by jkahvedj ### ########.fr */
/* */
/* ************************************************************************** */
/*
** LIBRARY: none
**
** DESCRIPTION:
** convert integer to string
*/
#include "libft.h"
static int ft_countdigits(long int n)
{
int count;
count = 0;
if (n == 0)
return (1);
while (n > 0)
{
n /= 10;
count++;
}
return (count);
}
static int ft_isnegative(long int n)
{
if (n < 0)
return (1);
else
return (0);
}
char *ft_itoa(int n)
{
char *str_nbr;
int count_digits;
int count_aux;
int is_negative;
long int n_aux;
n_aux = n;
is_negative = ft_isnegative(n_aux);
if (is_negative == 1)
n_aux *= -1;
count_digits = ft_countdigits(n_aux);
count_aux = count_digits;
str_nbr = malloc(sizeof(char) * (count_digits + is_negative + 1));
if (!str_nbr)
return (NULL);
if (is_negative == 1)
str_nbr[0] = '-';
while (count_digits > 0)
{
str_nbr[count_digits - 1 + is_negative] = (n_aux % 10) + '0';
n_aux /= 10;
count_digits--;
}
str_nbr[count_aux + is_negative] = '\0';
return (str_nbr);
}