-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strmapi.c
59 lines (52 loc) · 1.04 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
/**
* ft_strmapi():
* -------------
* Applies the function ’f’ to each character of the string ’s’
* to create a new string (with malloc(3)) resulting from successive
* applications of ’f’.
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int i;
char *str_copy;
i = 0;
/*
* Allocate the memory for str_copy and check if the
* allocation happened
*/
str_copy = (char *)malloc(strlen(s) + 1);
if (!str_copy || !s || !f)
{
return (NULL);
}
/* Apply and copy the function (f) to each memory location in str_copy*/
while (s[i] != '\0')
{
str_copy[i] = f(i, s[i]);
i++;
}
str_copy[i] = '\0';
return (str_copy);
}
/*
* NOTE:
* =====
* The following example is from: https://velog.io/@jungjaedev/ftstrmapi
*💾Example:
*/
char f(unsigned int i, char c)
{
char str;
str = c + 1;
return (str);
}
int main()
{
char str1[] = "abc";
char* str2;
str2 = ft_strmapi(str1, *f);
printf("%s\n", str2);
}