-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
40 lines (36 loc) · 1.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jados-sa <jados-sa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/20 21:49:10 by jados-sa #+# #+# */
/* Updated: 2024/10/29 19:37:06 by jados-sa ### ########.fr */
/* */
/* ************************************************************************** */
/* Applies the function f to each character of th string s, passing its index *
* as the first arguument and the character itself as the second. *
* A new string is created using malloc to collect the results from the *
* successive applications of f. *
* s: The string on which to iterate *
* f: The function to apply each character */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
size_t i;
char *str;
if (!s)
return (NULL);
str = (char *)malloc((ft_strlen(s) + 1) * sizeof(char));
if (!str)
return (NULL);
i = 0;
while (s[i])
{
str[i] = f(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}