-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strmapi.c
66 lines (56 loc) · 1.88 KB
/
ft_strmapi.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edetoh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/19 15:34:49 by edetoh #+# #+# */
/* Updated: 2024/10/25 11:18:41 by edetoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* ft_strmapi applies a function to each character.
* Takes 's' (source string) and 'f' (function to apply).
* Returns a new modified string or NULL on error.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *str_mod;
int str_len;
int i;
str_len = ft_strlen((char *)s);
str_mod = malloc((str_len + 1) * sizeof(char));
if (!str_mod)
return (NULL);
i = 0;
while (i < str_len)
{
str_mod[i] = f(i, s[i]);
i++;
}
str_mod[i] = 0;
return (str_mod);
}
// char i_love_alphabet(unsigned int index, char c)
// {
// int i;
// char letter;
// (void)c;
// i = index % 26;
// letter = 'a' + i;
// return (letter);
// }
// #include <string.h>
// #include <stdio.h>
// #include <unistd.h>
// int main(void)
// {
// char *str = "Bonzour je suis zeliamzlezbgzclementzarretezdezcopierzmonzgit";
// char *res = ft_strmapi(str, i_love_alphabet);
// printf("===== RESULT =====\n");
// printf("%s\n", res);
// printf("==================\n");
// return (1);
// }