-
Notifications
You must be signed in to change notification settings - Fork 4
/
ft_itoa.c
47 lines (42 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vbrazhni <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/02 21:17:03 by vbrazhni #+# #+# */
/* Updated: 2018/07/02 21:17:04 by vbrazhni ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_numlen(int n, int minus)
{
int numlen;
numlen = 1;
while ((n /= 10))
numlen++;
return (numlen + minus);
}
char *ft_itoa(int n)
{
char *str;
int numlen;
int minus;
int digit;
minus = (n < 0) ? 1 : 0;
numlen = ft_numlen(n, minus);
if ((str = ft_strnew(numlen)))
{
str[numlen--] = '\0';
while (numlen >= minus)
{
digit = n % 10;
str[numlen--] = FT_ABS(digit) + '0';
n /= 10;
}
if (minus)
str[0] = '-';
}
return (str);
}