-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
42 lines (39 loc) · 1.62 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmaurer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/06 20:56:42 by mmaurer #+# #+# */
/* Updated: 2021/09/07 22:20:47 by mmaurer ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* Applies the function ’f’ to each character of thestring ’s’ to create a new
* string (with malloc(3))resulting from successive applications of ’f’.
1. It takes two parameters: a string and a function pointer.
2. It allocates memory for a new string that is as long as the input string.
3. It loops through the input string and calls the function pointer for each
character.
4. It returns the new string.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *new;
size_t i;
i = 0;
if (!s || !f)
return (NULL);
new = (char *)malloc(sizeof(char) * ft_strlen(s) + 1);
if (!new)
return (NULL);
while (s[i] != '\0')
{
new[i] = f((unsigned int)i, s[i]);
++i;
}
new[i] = '\0';
return (new);
}