-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
39 lines (36 loc) · 1.3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thifranc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/16 11:05:38 by thifranc #+# #+# */
/* Updated: 2016/02/20 16:29:30 by thifranc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
char *out;
int i;
int j;
int x;
i = 0;
x = 0;
j = ft_strlen(s) - 1;
if (!(out = (char*)malloc(sizeof(char) * (ft_strlen(s) + 1))))
return (NULL);
while (s[i] && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n'))
i++;
while ((s[j] == ' ' || s[j] == '\t' || s[j] == '\n') && j != -1)
j--;
while (i <= j)
{
out[x] = s[i];
x++;
i++;
}
out[x] = '\0';
return (out);
}