-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strmapi.c
More file actions
56 lines (51 loc) · 1.6 KB
/
ft_strmapi.c
File metadata and controls
56 lines (51 loc) · 1.6 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
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sarfreit <sarfreit@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/22 17:42:43 by sarfreit #+# #+# */
/* Updated: 2025/10/22 17:42:43 by sarfreit ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
unsigned int len;
char *new_str;
if (!s || !f)
return (NULL);
i = 0;
len = ft_strlen(s);
new_str = (char *) malloc(len + 1);
if (!new_str)
return (NULL);
while (i < len)
{
new_str[i] = f(i, s[i]);
i++;
}
new_str[i] = '\0';
return (new_str);
}
/*
static char make_upper_even(unsigned int i, char c)
{
if ((i % 2 == 0) && (c >= 'a') && (c <= 'z'))
c -= 32;
if ((i % 2 != 0) && (c >= 'A') && (c <= 'Z'))
c += 32;
return (c);
}
#include <stdio.h>
int main(void)
{
char *s = "Hello THERE, how are YOU?";
char *new_str = ft_strmapi(s, make_upper_even);
printf("%s\n", new_str);
free(new_str);
return (0);
}
*/