-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
42 lines (38 loc) · 1.46 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: aviholai <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/25 14:34:42 by aviholai #+# #+# */
/* Updated: 2022/02/09 16:43:00 by aviholai ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** 'Strmapi()' (string map index) applies the parameter function 'f' to each
** character of specific index of the applied *string 's' to create a new
** string, which is a result of running the function on 's'.
** Returns the modified string.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
char *str;
if (s)
{
i = 0;
str = (char *)malloc(sizeof(char) * (ft_strlen(s)) + 1);
if (str == NULL)
return (NULL);
while (s[i] != '\0')
{
str[i] = f(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}
return (NULL);
}