-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strmapi_explained.c
More file actions
88 lines (78 loc) · 2.58 KB
/
ft_strmapi_explained.c
File metadata and controls
88 lines (78 loc) · 2.58 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi_explained.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acastrov <acastrov@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 16:27:26 by acastrov #+# #+# */
/* Updated: 2024/09/30 18:09:25 by acastrov ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
// Takes an ft as an argument and applies it in s string, making a new string
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
// We take a pointer to a ft as an argument, this ft has to recieve the same arguments as defined in ft_mapi.c (unsigned int and char)
{
char *dest_string;
unsigned int len;
unsigned int i;
len = (unsigned int)strlen(s); // We find the size of s as per usual
i = 0; // An iterative int to navigate the array
dest_string = malloc(len + 1); // Malloc with '\0' terminator for the result
if (!dest_string)
return (NULL);
while (i < len)
{
dest_string[i] = f(i, s[i]); // We apply f (ft taked as an argument) to every char in s and we allocate in a new string
i++;
}
dest_string[i] = '\0'; // When using malloc, always null terminate the new string!!!!
return (dest_string);
}
char upper_par(unsigned int i, char c)
{
if (i % 2 == 0)
return toupper(c);
return c;
}
char upper_impar(unsigned int i, char c)
{
if (!(i % 2 == 0))
return toupper(c);
return c;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// First, we use upper_par
char *str1 = "hello, world!";
char *result1 = ft_strmapi(str1, upper_par);
if (result1)
{
printf("Original string: %s\n", str1);
printf("Modified string: %s\n", result1);
free(result1);
}
else
{
printf("ft_strmapi returned NULL\n");
}
// Then, we use upper_impar using the same ft_strmapi
char *str2 = "hello, world!";
char *result2 = ft_strmapi(str2, upper_impar);
if (result2)
{
printf("Original string: %s\n", str2);
printf("Modified string: %s\n", result2);
free(result2);
}
else
{
printf("ft_strmapi returned NULL\n");
}
return 0;
}