libft
src
ft_itoa.c
Go to the documentation of this file.
1
/* ************************************************************************** */
3
/* */
4
/* ::: :::::::: */
5
/* ft_itoa.c :+: :+: :+: */
6
/* +:+ +:+ +:+ */
7
/* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8
/* +#+#+#+#+#+ +#+ */
9
/* Created: 2019/09/05 14:16:34 by unite #+# #+# */
10
/* Updated: 2020/07/16 03:04:24 by unite ### ########.fr */
11
/* */
12
/* ************************************************************************** */
13
14
#include "
libft.h
"
15
#include <stdlib.h>
16
#include <limits.h>
17
18
static
size_t
ft_int_len(
int
n)
19
{
20
size_t
i;
21
22
i = 1;
23
while
(n /= 10)
24
i++;
25
return
(i);
26
}
27
32
char
*
ft_itoa
(
int
n)
33
{
34
char
*str;
35
size_t
len;
36
int
tmp;
37
38
len = ft_int_len(n);
39
tmp = n < 0 ? n : -n;
40
if
(n < 0)
41
len++;
42
if
(!(str =
ft_strnew
(len)))
43
return
(NULL);
44
while
(tmp)
45
{
46
str[--len] = -(tmp % 10) +
'0'
;
47
tmp /= 10;
48
}
49
if
(n < 0)
50
str[--len] =
'-'
;
51
return
(str);
52
}
ft_itoa
char * ft_itoa(int n)
Replicates behaviour of itoa from libc
Definition:
ft_itoa.c:32
libft.h
ft_strnew
char * ft_strnew(size_t size)
Allocates a fresh string.
Definition:
ft_strnew.c:25
Generated by
1.8.16