-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
65 lines (59 loc) · 1.77 KB
/
ft_strtrim.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ralee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/10 17:54:22 by ralee #+# #+# */
/* Updated: 2017/12/15 11:56:47 by ralee ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ifchecker(int *index, char const *s, int *end)
{
while (s[*index] != '\0')
{
if ((s[*index + 1] == 32 || s[*index + 1] == '\n' || s[*index + 1] ==
'\t' || s[*index + 1] == 0) && (s[*index] != 32 &&
s[*index] != '\n' && s[*index] != '\t'))
*end = *index;
(*index)++;
}
}
static void strsetter(char **str, int *start, int *end, char const *s)
{
int index;
index = 0;
while (*start < *end + 1)
{
str[0][index] = s[*start];
index++;
(*start)++;
}
str[0][index] = '\0';
}
char *ft_strtrim(char const *s)
{
int start;
int end;
int index;
char *str;
str = 0;
if (s)
{
start = 0;
while (s[start] == 32 || s[start] == '\n' || s[start] == '\t')
start++;
index = start;
end = index;
ifchecker(&index, s, &end);
str = (char*)malloc(end - start + 2);
if (str)
{
strsetter(&str, &start, &end, s);
}
return (str);
}
return (0);
}