-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
76 lines (69 loc) · 2.02 KB
/
ft_strtrim.c
File metadata and controls
76 lines (69 loc) · 2.02 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simarcha <simarcha@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/17 09:48:32 by simarcha #+# #+# */
/* Updated: 2024/01/26 19:08:37 by simarcha ### ########.fr */
/* */
/* ************************************************************************** */
//for ft_strtrim function: if set or the string doesn't exist, just return NULL
//we create the function check char to know if the word is in our string
//#include <stddef.h>
//#include <stdlib.h>
#include "libft.h"
static int check_char(char const *s1, char c)
{
int i;
i = 0;
while (s1[i] != '\0')
{
if (s1[i] == c)
return (1);
i++;
}
return (0);
}
static size_t len(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t start;
size_t end;
size_t i;
start = 0;
while ((s1[start] != '\0') && (check_char(set, s1[start]) == 1))
start++;
end = len(s1);
while ((end > start) && (check_char(set, s1[end - 1]) == 1))
end--;
str = ft_calloc(sizeof(char), (end - start + 1));
if (!(str))
return (NULL);
i = 0;
while (start < end)
str[i++] = s1[start++];
str[i] = 0;
return (str);
}
/*
#include <stdio.h>
int main(void)
{
const char s1[100] = "abababHolaMundoababab";
char const set[100] = "ab";
char *str;
str = ft_strtrim(s1, set);
printf("len empty string = %zu\n", len(set));
printf("final string : __%s__\n", str);
return(0);
}*/