ft_strmapi
Subject
FT_STRMAPI() (simplified)
NAME
ft_strmapi -- apply a function to each character of a string
SYNOPSIS
char *ft_strmapi(const char *s, char (*f)(unsigned int, char));
DESCRIPTION
Apply the function 'f' to each characters in the string 's' to create a new string (with malloc(3)) resulting of the successive applications of 'f'.
PARAMETERS
s: string over which to iterate
f: function to apply to each character
RETURN VALUES
ft_strmapi() returns a new string resulting of the successive applications of 'f'; NULL if the memory allocations failed.
AUTHORIZED EXTERNAL FUNCTIONS
malloc(3)
Understandable explanation
This functions takes two parameters, the first one is a string, and the second one is a function.
What ft_strmapi
does is apply the function f
to every character of the string s
.
It passes the index of the character in the string, and the character to the function f
.
The result of the function f
is placed in the new string at index i
.
At the end, we return the new string resulting of the application of f
on every character of the string.
Hints
First, we have to allocate enough memory for the whole string plus one for the NUL-terminating character.
Then we can loop over the string s
, and call the function f
on each character of s
.
Commented solution
Last updated
Was this helpful?