split whitespaces

This commit is contained in:
Jack Halford 2016-09-27 05:14:32 +02:00
parent c754d1f1bb
commit ddb24369c9
3 changed files with 128 additions and 4 deletions

View file

@ -105,19 +105,25 @@ void ft_lst_merge(t_list **begin_list1, t_list *begin_list2);
void ft_lst_reverse(t_list **begin_list);
int ft_diff(void *a, void *b);
char *ft_strrev(char *str);
t_list *ft_id(t_list *a);
char *ft_strrev(char *str);
char **ft_strsplit(char const *s, char c);
char **ft_split_whitespaces(char *str);
char *ft_convert_base(char *str, char *base_from, char *base_to, char *flags);
char *ft_itoa_base(int nbr, char *base, char *flags);
char *ft_lltoa_base(long long nbr, char *base, char *flags);
char *ft_ulltoa_base(unsigned long long nbr, char *base);
char *ft_uitoa_base(unsigned int nbr, char *base);
void ft_strlsort(char **list, int size, int (*cmp)());
char *ft_convert_base(char *str, char *base_from, char *base_to, char *flags);
size_t ft_ilen(int n);
size_t ft_uilen(unsigned int n);
void ft_strlsort(char **list, int size, int (*cmp)());
void ft_strlprint(char **strl, char sep);
int ft_time_isrecent(time_t event);
int ft_xattr_print(char *path);
int ft_xattr_count(char *path);

View file

@ -0,0 +1,108 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_whitespaces.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/08 09:33:37 by jhalford #+# #+# */
/* Updated: 2016/08/20 23:23:41 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#define SEP(x) (x==' ' || x=='\n' || x=='\t')
char **alloc_table(char **table, char *str)
{
int i;
int n_words;
i = 0;
n_words = 0;
while (SEP(str[i]))
i++;
while (str[i] != '\0')
{
i++;
if (SEP(str[i]))
{
n_words++;
while (SEP(str[i]))
i++;
}
}
if (!SEP(str[i - 1]))
n_words++;
table = (char**)malloc(sizeof(*table) * (n_words + 1));
table[n_words] = 0x0;
return (table);
}
char **alloc_words(char **table, char *str)
{
int i;
int j;
int k;
i = 0;
j = 0;
k = 0;
while (SEP(str[i]))
i++;
while (str[i] != '\0')
{
i++;
if (SEP(str[i]) || str[i] == '\0')
{
table[j] = (char*)malloc(sizeof(**table) * (k + 1));
j++;
k = 0;
while (SEP(str[i]))
i++;
}
k++;
}
return (table);
}
char **fill_table(char **table, char *str)
{
int i;
int j;
int k;
i = 0;
j = 0;
k = 0;
while (SEP(str[i]))
i++;
while (str[i] != '\0')
{
table[j][k] = str[i];
i++;
k++;
if (SEP(str[i]))
{
table[j][k] = '\0';
j++;
k = 0;
while (SEP(str[i]))
i++;
}
}
return (table);
}
char **ft_split_whitespaces(char *str)
{
char **table;
table = NULL;
table = alloc_table(table, str);
table = alloc_words(table, str);
table = fill_table(table, str);
return (table);
}

View file

@ -0,0 +1,10 @@
#include "libft.h"
void ft_strlprint(char **strl, char sep)
{
int i;
i = 0;
while (strl[i])
ft_printf("%s%c", strl[i], sep);
}