-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
75 lines (68 loc) · 1.79 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
66
67
68
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amatshiy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/06/11 10:51:12 by amatshiy #+# #+# */
/* Updated: 2017/06/11 11:08:26 by amatshiy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_first_space(char const *str)
{
size_t x;
size_t count;
x = 0;
count = 0;
while (str[x] && (str[x] == 32 || str[x] == '\n' || str[x] == '\t'))
{
count++;
x++;
}
return (count);
}
static size_t ft_end_space(char const *str)
{
size_t x;
size_t count;
size_t len;
len = ft_strlen(str);
x = len - 1;
if (len == 0)
return (0);
count = 0;
while (x > 0 && (str[x] == 32 || str[x] == '\t' || str[x] == '\n'))
{
count++;
x--;
}
return (count);
}
char *ft_strtrim(char const *s)
{
char *str;
size_t x;
size_t y;
size_t lastspace;
size_t len;
if (s == NULL)
return (NULL);
x = ft_first_space(s);
lastspace = ft_end_space(s);
len = ft_strlen(s);
if (len - x <= 0)
return (ft_strdup(""));
y = 0;
str = ft_strnew(len - x - lastspace);
if (!str)
return (NULL);
while (s[x] != '\0' && x < len - lastspace)
{
str[y] = s[x];
x++;
y++;
}
return (str);
}