-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
95 lines (87 loc) · 1.95 KB
/
Copy pathft_strtrim.c
File metadata and controls
95 lines (87 loc) · 1.95 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
93
94
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dpadenko <dpadenko@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/11 18:20:02 by dpadenko #+# #+# */
/* Updated: 2023/09/15 22:37:29 by dpadenko ### ########.fr */
/* */
/* ************************************************************************** */
//#include <stdio.h>
//#include <stdlib.h>
#include "libft.h"
static int ft_strtrim_start(char const *s1, char const *set)
{
int i;
int j;
int start;
i = 0;
start = 1;
if (*set == '\0')
return (0);
while (start)
{
j = 0;
while (set[j] != '\0')
{
start = 0;
if (s1[i] == set[j])
{
i++;
start = 1;
break ;
}
j++;
}
}
return (i);
}
static int ft_strtrim_end(char const *s1, char const *set)
{
int i;
int j;
int end;
i = ft_strlen((char *)s1) - 1;
end = 1;
if (*set == '\0')
return (i);
while (end)
{
j = 0;
while (set[j] != '\0')
{
end = 0;
if (s1[i] == set[j])
{
i--;
end = 1;
break ;
}
j++;
}
}
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
int start;
int len;
start = ft_strtrim_start(s1, set);
len = ft_strtrim_end (s1, set) - start + 1;
return (ft_substr (s1, start, len));
}
/*
int main (void)
{
char a[] = " ,,Copy me, again and again!,, ";
char b[] = " ,";
char *trimmed = ft_strtrim(a, b);
if (trimmed != NULL)
{
printf("%s\n", trimmed);
free(trimmed);
}
}
*/