Repository navigation
Expand file tree
/
Copy pathft_strmapi.c
More file actions
92 lines (81 loc) · 2.48 KB
/
Copy pathft_strmapi.c
File metadata and controls
92 lines (81 loc) · 2.48 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
89
90
91
92
/* ************************************************************************** */
/* */
/* /#/ |#/|#| */
/* ft_strmapi.c /#/ |/ |#| */
/* /#/ /#/ */
/* By: nephco <nephco@student.42.fr> /#/ 0x2A /#/ */
/* /#/____ |#| /| */
/* Created: 2026/06/16 04:03:16 by nephco |#######| |#|/#| */
/* Updated: 2026/08/10 04:42:02 by nephco |#| NEPH */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stddef.h>
/* ========================================================================== */
static size_t ft_strlen(const char *chain)
{
const char *value;
value = chain;
while(*value)
{
value++;
}
return (value - chain);
}
// -------------------------------------------------------------------------- //
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
unsigned int count;
size_t length;
char *value;
if (!s || !f)
return (NULL);
length = ft_strlen(s);
value = (char *)malloc(sizeof(char) * (length + 1));
if (!value)
return (NULL);
count = 0;
while(count < length)
{
value[count] = f(count, s[count]);
count++;
}
value[count] = '\0';
return (value);
}
/* ========================================================================== */
#include <stdio.h>
static char ft_toupper(char c)
{
if (c >= 'a' && c <= 'z')
return (c - 32);
return (c);
}
static char up_adapter(unsigned int index, char c)
{
(void)index;
return ((char)ft_toupper(c));
}
static void display(char *play_s)
{
char *result;
printf("Avant : `%s`\n", play_s);
result = ft_strmapi(play_s, up_adapter);
if (!result)
return;
printf("Apres : `%s`\n", result);
free(result);
}
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("merci de saisir un argument\n");
return (1);
}
display(argv[1]);
return (0);
}
/* ========================================================================== */
/* END ====================================================================== */
/* ========================================================================== */