-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
38 lines (34 loc) · 1.4 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: jaberkro <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/10/26 17:26:27 by jaberkro #+# #+# */
/* Updated: 2021/12/01 13:07:59 by jaberkro ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
int counter;
char *out;
counter = 0;
if (s == NULL)
return (NULL);
out = ft_calloc(ft_strlen(s) + 1, 1);
if (out == NULL)
return (NULL);
while (s[counter])
{
out[counter] = f(counter, s[counter]);
counter++;
}
return (out);
}
/* Applies the function ’f’ to each character of
the string ’s’ , and passing its index as first
argument to create a new string (with malloc(3))
resulting from successive applications of ’f’.
*/