-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strmapi.c
64 lines (55 loc) · 2.21 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sperez-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/23 11:04:31 by sperez-p #+# #+# */
/* Updated: 2021/08/23 11:26:04 by sperez-p ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
NAME
****
ft_strmapi ---> iterate string aplying a function to each character
SYNOPSIS
********
#include "mylibft.h"
char *ft_strmapi(const char *str, char (*f)(unsigned int, char));
PARAMETERS
**********
str ---> the string on which to iterate
f ---> the function to apply to each character
DESCRIPTION
***********
The ft_strmapi() function applies the function 'f' to each character of the
string 'str' to create a new string resulting from successive applications of
the function. This new string will be allocated in a dinamic memory space using
the malloc() C function.
It first checks if 'str' is not NULL and then reserves the memory space for the
new string 'dst', which will be the same size as the original string.
Once verified that there is no error allocating the memory, it iterates 'str'
assigning to each space of 'dst' the return value of applying the function 'f'
to each character of 'str'.
RETURN VALUE
************
- A pointer to he new string.
- If there is an error allocating memory or if 'str' is NULL, returns NULL.
*/
char *ft_strmapi(const char *s, char (*f)(unsigned int, char))
{
char *dest;
size_t i;
i = -1;
if (!s)
return (NULL);
dest = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1));
if (!dest)
return (NULL);
while (s[++i])
dest[i] = f(i, ((char *)s)[i]);
dest[i] = '\0';
return (dest);
}