Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_strmapi.c File Reference
+ Include dependency graph for ft_strmapi.c:

Go to the source code of this file.

Functions

char * ft_strmapi (char const *s, char(*f)(unsigned int, char))
 Maps a function over a string into a new allocated string.
 

Function Documentation

◆ ft_strmapi()

char * ft_strmapi ( char const *  s,
char(*)(unsigned int, char)  f 
)

Maps a function over a string into a new allocated string.

Parameters
sInput string.
fMapping callback receiving index and character.
Returns
Newly allocated mapped string, or NULL on failure.

Definition at line 22 of file ft_strmapi.c.

23{
24 char *str;
25 unsigned int i;
26 size_t len;
27
28 if (!s || !f)
29 return (NULL);
30 len = ft_strlen(s);
31 str = (char *)malloc(sizeof(char) * (len + 1));
32 if (!str)
33 return (NULL);
34 i = 0;
35 while (s[i])
36 {
37 str[i] = f(i, s[i]);
38 i++;
39 }
40 str[i] = '\0';
41 return (str);
42}
size_t ft_strlen(const char *str)
Returns the length of a null-terminated string.
Definition ft_strlen.c:21

References ft_strlen().

+ Here is the call graph for this function: