-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_util_string_sub.c
67 lines (60 loc) · 1.08 KB
/
ft_util_string_sub.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
/* ************************************************************************** */
/**//**//*****//**//* By: *//**//* Created: by *//* Updated: by *//**/
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
t_itoa_info it;
it.len = !n;
it.n_backup = n;
while (it.n_backup)
{
it.len++;
it.n_backup /= 10;
}
it.len += (n < 0);
it.temp = ft_calloc(it.len + 1, sizeof(char));
if (it.temp)
{
if (n < 0)
it.temp[0] = '-';
else if (!n)
it.temp[--it.len] = '0';
while (n)
{
it.temp[--it.len] = '0' + ((1 - ((n < 0) << 1)) * (n % 10));
n /= 10;
}
}
return (it.temp);
}
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
size_t i;
char *temp;
if (!s || !f)
return (NULL);
temp = ft_calloc(ft_strlen(s) + 1, sizeof(char));
if (temp)
{
i = 0;
while (s[i])
{
temp[i] = f(i, s[i]);
i++;
}
}
return (temp);
}
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
size_t i;
if (!s || !f)
return ;
i = 0;
while (s[i])
{
f(i, &s[i]);
i++;
}
}