Cub3D/libft/str/ft_strmapi.c
2024-10-31 18:17:43 +01:00

43 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:56:57 by rparodi #+# #+# */
/* Updated: 2024/10/31 18:14:29 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @brief Applies a function to each character of a string.
*
* @param s The input string.
* @param f The function to apply to each character.
*
* @return A pointer to the newly created string,
* or NULL if memory allocation fails.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
size_t i;
size_t len;
char *str;
i = 0;
len = ft_strlen(s);
str = (char *)malloc(len + 1);
if (!str)
return (NULL);
else
ft_bzero((char *)str, len + 1);
while (i < len)
{
str[i] = f(i, s[i]);
i++;
}
return (str);
}