-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strmap.c
42 lines (39 loc) · 1.33 KB
/
ft_strmap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/11 21:57:10 by dskrypny #+# #+# */
/* Updated: 2017/11/18 14:31:00 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmap(char const *s, char (*f)(char))
{
int i;
int j;
char *res;
char *res1;
i = 0;
j = 0;
if (s == NULL || f == NULL)
return (NULL);
res = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1));
if (!res)
return (NULL);
while (s[i])
{
if (f(s[i]) != 0)
res[j++] = f(s[i]);
i++;
}
res[j] = 0;
i = -1;
res1 = (char *)malloc(sizeof(char) * j);
while (res[++i])
res1[i] = res[i];
res1[i] = 0;
return (res1);
}