-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
44 lines (41 loc) · 1.6 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vcodrean <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 19:30:06 by vcodrean #+# #+# */
/* Updated: 2022/10/01 10:20:04 by vcodrean ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* Ft_strmapi() applies the function f to each character of the string passed
* as argument by giving its index as first argument to create a “fresh”
* new string (with malloc(3)) resulting from the * successive applications of f.
*
* param char s The string to be mapped.
* param char f a function that takes an unsigned int and a char as parameters
* and returns a char.
*
* return A pointer to a new string.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
int i;
char *str;
if (!s)
return (NULL);
str = malloc(sizeof(char) * (ft_strlen(s) + 1));
if (!str)
return (NULL);
i = 0;
while (s[i] != '\0')
{
str[i] = f(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}