-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strmapi.c
More file actions
42 lines (38 loc) · 1.52 KB
/
Copy pathft_strmapi.c
File metadata and controls
42 lines (38 loc) · 1.52 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
42
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: antandre <antandre@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 10:48:04 by antandre #+# #+# */
/* Updated: 2024/06/17 14:34:57 by antandre ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
/*
* Applies the function 'f' to each character of the string
* 's', passing its index as the first argument and the
* character itself as the second argument. A new string
* (allocated using malloc(3)) is created to collect
* the results of the successive applications of 'f'.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
char *result;
if (s == NULL)
return (NULL);
i = 0;
result = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1));
if (result == NULL)
return (NULL);
while (s[i])
{
result[i] = (f)(i, s[i]);
i++;
}
result[i] = '\0';
return (result);
}