-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
67 lines (60 loc) · 846 Bytes
/
utils.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
#include "printf.h"
int ft_max_int(int a, int b)
{
if (a >= b)
return (a);
else
return (b);
}
void ft_putstr_count(char *s, int *count)
{
if (!s)
return ;
while (*s)
{
ft_putchar_fd(*s, 1);
s++;
(*count)++;
}
}
void ft_putchar_count(char c, int *count)
{
ft_putchar_fd(c, 1);
(*count)++;
}
int count_digits_u(long n)
{
int counter;
if (n == 0)
return (1);
counter = 0;
while (n != 0)
{
n /= 10;
counter++;
}
return (counter);
}
char *ft_itoa_u(unsigned int n)
{
char *res;
int len;
int pos;
long num;
num = n;
pos = num >= 0;
len = count_digits(num) + (!pos ? 1 : 0);
num *= pos ? 1 : -1;
if (!(res = malloc(sizeof(char) * (len + 1))))
return (NULL);
res[len] = '\0';
while (len >= 0)
{
res[len - 1] = num % 10 + 48;
num /= 10;
len--;
}
if (!pos)
res[0] = '-';
return (res);
}