-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
51 lines (46 loc) · 1.45 KB
/
ft_strmapi.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/11 21:57:45 by dskrypny #+# #+# */
/* Updated: 2017/11/18 14:40:10 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_cut(char *res, char *res1)
{
int i;
i = -1;
while (res[++i])
res1[i] = res[i];
res1[i] = 0;
free(res);
return (res1);
}
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
int i;
int j;
char *res;
char *res1;
i = 0;
j = 0;
if (s == NULL || f == NULL)
return (NULL);
res = malloc(sizeof(char) * (ft_strlen(s) + 1));
if (!res)
return (NULL);
while (s[i])
{
if (f(i, s[i]) != 0)
res[j++] = f(i, s[i]);
i++;
}
res[j] = 0;
res1 = (char *)malloc(sizeof(char) * j);
res1 = ft_cut(res, res1);
return (res1);
}