-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_strmapi.c
38 lines (34 loc) · 1.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adiaz-lo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/13 11:42:22 by adiaz-lo #+# #+# */
/* Updated: 2020/01/13 11:56:40 by adiaz-lo ### ########.fr */
/* */
/* ************************************************************************** */
/*
** This function applies the function 'f()' to each character of the string
** ('s') passed as argument to create a new string resulting from successive
** applications of 'f()'.
*/
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
char *str;
if (!s || !f)
return (NULL);
if (!(str = malloc(sizeof(char) * (ft_strlen(s) + 1))))
return (0);
i = 0;
while (s[i])
{
str[i] = f(i, s[i]);
i++;
}
str[i] = 0;
return (str);
}