-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
58 lines (51 loc) · 1.67 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: estettle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/03 10:55:58 by estettle #+# #+# */
/* Updated: 2024/10/03 10:56:21 by estettle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* ft_strmapi()
* iterates through the string *s, applying the f() function to each character
* and storing the result into a new string.
* returns the new string with altered contents.
*/
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
int i;
int s_length;
char *result_s;
i = 0;
s_length = ft_strlen(s);
result_s = malloc((s_length + 1) * sizeof(char));
if (!result_s)
return (NULL);
while (i < s_length)
{
result_s[i] = f(i, s[i]);
i++;
}
result_s[i] = '\0';
return (result_s);
}
/*
#include <stdio.h>
char ft_uppercase(unsigned int index, char c)
{
(void)index;
if (c >= 'a' && c <= 'z')
c -= 32;
return (c);
}
int main(void)
{
char (*f)(unsigned int, char);
f = ft_uppercase;
printf("%s\n", ft_strmapi("Can. You. Hear. Me?", f));
}
*/