-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr_itoa.c
65 lines (59 loc) · 1.52 KB
/
pr_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pr_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zwalad <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/16 12:50:16 by zwalad #+# #+# */
/* Updated: 2022/08/21 00:14:08 by zwalad ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int len_len(int len, long n, int s)
{
while (n != 0)
{
len++;
n /= 10;
}
if (s < 0)
return (len + 1);
return (len);
}
static char *ft_tetoi(char *str, long num, int s)
{
int len;
len = 0;
len = len_len(len, num, s);
str = malloc(len + 1);
if (!str)
return (NULL);
str[len] = '\0';
while (len--)
{
str[len] = num % 10 + '0';
num = num / 10;
}
if (s < 0)
str[0] = '-';
return (str);
}
char *pr_itoa(int n)
{
char *str;
long num;
int s;
s = 1;
num = n;
str = 0;
if (num < 0)
{
num *= -1;
s = -1;
}
if (num == 0)
return (ft_strdup("0"));
str = ft_tetoi(str, num, s);
return (str);
}