-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strmapi.c
More file actions
43 lines (40 loc) · 1.62 KB
/
ft_strmapi.c
File metadata and controls
43 lines (40 loc) · 1.62 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jowagner <jowagner@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/29 14:51:08 by jowagner #+# #+# */
/* Updated: 2024/12/12 19:50:15 by jowagner ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @brief Applies a function to each character of a string,
* producing a new string with the transformed characters.
*
* @param s The input string to process.
* @param f The function to apply to each character of the string.
* It takes the index of the character and the character itself as arguments.
* @return A newly allocated string resulting from applying the function `f`
* to each character of `s`, or NULL if allocation fails.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *str;
size_t len;
unsigned int i;
len = ft_strlen(s);
str = malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
i = 0;
while (i < len)
{
str[i] = f(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}