-
Notifications
You must be signed in to change notification settings - Fork 10
/
ft_strmapi.c
37 lines (33 loc) · 1.29 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jaeskim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/27 05:16:39 by jaeskim #+# #+# */
/* Updated: 2020/09/30 19:43:08 by jaeskim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** ft_strmapi - Applies the function 'f' to each character of the
** string 's' to create a new string
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *result;
size_t len;
size_t i;
len = ft_strlen(s);
if (!(result = (char *)malloc(sizeof(char) * (len + 1))))
return (0);
i = 0;
while (i < len)
{
result[i] = f(i, s[i]);
i++;
}
result[len] = '\0';
return (result);
}