aboutsummaryrefslogtreecommitdiff
path: root/ft_strmapi.c
blob: da75c9850091f3dda45b40516d9b836503597879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdlib.h>
#include "libft.h"

char    *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
    size_t  i;
    size_t  len;
    char    *mapped;

    len = ft_strlen(s);
    if ((mapped = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
        return (NULL);
    i = 0;
    while (i < len)
    {
        mapped[i] = (*f)((unsigned int)i, s[i]);
        i++;
    }
    mapped[i] = '\0';
    return (mapped);
}