libft/ft_strtrim.c
2018-04-07 17:50:14 +02:00

42 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/07 17:00:34 by tmaze #+# #+# */
/* Updated: 2018/04/07 17:48:44 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
int in_word;
size_t i;
size_t start;
size_t len;
i = 0;
start = 0;
len = 0;
while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
i++;
start = i;
in_word = 1;
while (s[i])
{
if (in_word && (!ft_isprint(s[i]) || s[i] == ' '))
{
len = i - start;
in_word = 0;
}
else if (!in_word && ft_isprint(s[i]) && s[i] != ' ')
in_word = 1;
i++;
}
return (ft_strsub(s, start, len));
}