-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strmapi.c
More file actions
41 lines (38 loc) · 1.4 KB
/
ft_strmapi.c
File metadata and controls
41 lines (38 loc) · 1.4 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rdragan <rdragan@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/05 16:38:55 by rdragan #+# #+# */
/* Updated: 2022/11/05 16:45:25 by rdragan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
Applies the function f to each character of s and
pass it's index as the first argument to create a new string.
The new string will be returned.
@param s: string on which to iterate.
@param f: function to apply.
*/
char *ft_strmapi(const char *s, char (*f)(unsigned int, char))
{
char *new;
size_t len;
size_t i;
if (!s)
return (NULL);
len = ft_strlen(s);
new = malloc((len + 1) * sizeof(char));
if (!new)
return (NULL);
i = -1;
while (s[++i])
{
new[i] = f(i, s[i]);
}
new[i] = '\0';
return (new);
}