-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
54 lines (48 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hecmarti <hecmarti@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 09:29:40 by hecmarti #+# #+# */
/* Updated: 2023/10/18 15:00:54 by hecmarti ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *mapi;
size_t i;
i = 0;
if (!s || !f)
return (NULL);
mapi = malloc(ft_strlen(s) + 1);
if (!mapi)
return (NULL);
while (s[i])
{
mapi[i] = f(i, s[i]);
i++;
}
mapi[i] = '\0';
return (mapi);
}
/*
int main(void)
{
const char *original_str = "Hello";
char *modified_str = ft_strmapi(original_str, &modify_char);
if (modified_str)
{
printf("Original string: %s\n", original_str);
printf("Modified string: %s\n", modified_str);
free(modified_str);
}
else
{
printf("Error: Memory allocation failed.\n");
}
return 0;
}
*/