This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_strmapi.c
45 lines (40 loc) · 1.47 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
43
44
45
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcombeau <mcombeau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/28 04:35:39 by mcombeau #+# #+# */
/* Updated: 2021/12/02 16:21:12 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
DESRIPTION :
The function ft_strmapi applies the given function f to each character
in the given string s and allocates sufficient memory to store the
resulting new string.
RETURN VALUE :
A pointer to the newly created string. NULL if the memory allocation
fails.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *str;
unsigned int i;
if (!s || (!s && !f))
return (ft_strdup(""));
else if (!f)
return (ft_strdup(s));
str = ft_strdup(s);
if (!str)
return (NULL);
i = 0;
while (s[i])
{
str[i] = (*f)(i, s[i]);
i++;
}
return (str);
}