-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strcute.c
executable file
·74 lines (65 loc) · 1.64 KB
/
ft_strcute.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
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kbensado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/22 20:51:22 by kbensado #+# #+# */
/* Updated: 2016/11/11 07:07:13 by kbensado ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_start(const char *s, char c)
{
int i;
i = 0;
while ((s[i] == c && s[i] != '\0'))
i++;
return (i);
}
static int ft_end(const char *s, char c)
{
int j;
j = 0;
while (s[j] != '\0')
j++;
j--;
while (s[j] == c)
j--;
return (j);
}
static char *one_char(const char *str)
{
char *tab;
tab = ft_strnew(1);
tab[0] = str[0];
return (tab);
}
char *ft_strcute(const char *s, char c)
{
int i;
int j;
int k;
char *tab;
if (!s)
return (NULL);
k = 0;
i = ft_start(s, c);
j = ft_end(s, c);
if (j - i <= 0)
j = i - 1;
if (j == -1)
return (one_char(s));
tab = (char *)malloc(sizeof(char) * (j - i + 2));
if (tab == NULL)
return (NULL);
while (i <= j)
{
tab[k] = s[i];
i++;
k++;
}
tab[k] = '\0';
return (tab);
}