-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
43 lines (38 loc) · 1.28 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amahla <amahla@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:35:50 by amahla #+# #+# */
/* Updated: 2022/05/03 10:06:52 by amahla ### ########.fr */
/* */
/* ************************************************************************** */
#include"libft.h"
static size_t slen(char const *str)
{
size_t i;
i = 0;
while (str && *(str + i))
i++;
return (i);
}
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
char *dst;
i = 0;
if (!s || !f)
return (NULL);
dst = malloc((slen(s) + 1) * sizeof(char));
if (!dst)
return (NULL);
while (s[i])
{
dst[i] = (*f)(i, s[i]);
i++;
}
dst[i] = '\0';
return (dst);
}