| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_strsplit.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/11/29 02:25:46 by bchanot #+# #+# */
- /* Updated: 2018/10/29 11:52:42 by bchanot ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "libft.h"
- static size_t ft_cpt(const char *s, char c)
- {
- size_t i;
- i = 0;
- while (*s)
- {
- while (*s && *s == c)
- s++;
- if (*s && *s != c)
- i++;
- while (*s && *s != c)
- s++;
- }
- return (i);
- }
- static size_t ft_word_len(const char *s, char c)
- {
- size_t len;
- len = 0;
- while (s[len] && s[len] != c)
- len++;
- if (!len)
- len++;
- return (len);
- }
- char **ft_strsplit(const char *s, char c)
- {
- char **t_str;
- size_t word;
- size_t i;
- word = 0;
- i = 0;
- if (!s)
- return (NULL);
- if (!(t_str = (char **)ft_memalloc(sizeof(char *) * (ft_cpt(s, c) + 1))))
- return (NULL);
- while (s[i])
- {
- if (s[i] == c)
- i++;
- else
- {
- t_str[word] = ft_strsub(&s[i], 0, ft_word_len(&s[i], c));
- i = i + ft_word_len(&s[i], c);
- word++;
- }
- }
- t_str[word] = NULL;
- return (t_str);
- }
|