123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_strsplit.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: mazimi <mazimi@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2014/12/08 19:26:52 by mazimi #+# #+# */
- /* Updated: 2015/03/04 12:01:35 by mazimi ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "libft.h"
- static int ft_count_words(const char *s, char c)
- {
- int sp;
- int sp_tmp;
- sp_tmp = 0;
- sp = 0;
- while (*s)
- {
- if (sp_tmp == 1 && *s == c)
- sp_tmp = 0;
- if (sp_tmp == 0 && *s != c)
- {
- sp_tmp = 1;
- sp++;
- }
- s++;
- }
- return (sp);
- }
- char **ft_strsplit(char const *s, char c)
- {
- int sp;
- char **tab;
- int i;
- int j;
- int start;
- if ((s == 0) || (c == 0))
- return (NULL);
- sp = ft_count_words(s, c);
- tab = malloc((sizeof(char *) * (sp + 1)));
- i = 0;
- j = -1;
- while (++j < sp)
- {
- while (s[i] && s[i] == c)
- i++;
- start = i;
- while (s[i] && s[i] != c)
- i++;
- tab[j] = malloc((sizeof(char *)) * ((i - start) + 1));
- tab[j] = ft_strsub(s, start, i - start);
- i++;
- }
- tab[j] = NULL;
- return (tab);
- }
|