Advertisement
eg0rmaffin

this strsplit is actually WORKS !!!

Sep 16th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include "libft.h"
  2.  
  3. int         ft_count_word(char const *s, char c)
  4. {
  5.     int     count;
  6.  
  7.     count = 0;
  8.     while (*s)
  9.     {
  10.         if (*s != c && (*(s + 1) == c || *(s + 1) == '\0'))
  11.             count++;
  12.         s++;
  13.     }
  14.     return (count);
  15. }
  16.  
  17. int         ft_word(char const *s, char c)
  18. {
  19.     int     count;
  20.  
  21.     count = 0;
  22.     while (*s != c && *s != '\0')
  23.     {
  24.         count++;
  25.         s++;
  26.     }
  27.     return (count);
  28. }
  29.  
  30. char        **ft_strsplit(char const *s, char c)
  31. {
  32.     int     i;
  33.     int     j;
  34.     char    **arr;
  35.  
  36.     i = 0;
  37.     if (!(arr = (char**)malloc(sizeof(char*) * (ft_count_word(s, c) + 1))))
  38.         return (NULL);
  39.     while (*s)
  40.     {
  41.         while (*s == c)
  42.             s++;
  43.         if (*s)
  44.         {
  45.             if (!(arr[i] = (char*)malloc(sizeof(char) * (ft_word(s, c) + 1))))
  46.                 return (NULL);
  47.             j = 0;
  48.             while (*s != c && *s)
  49.                 arr[i][j++] = *(s++);
  50.             arr[i][j] = '\0';
  51.             i++;
  52.         }
  53.     }
  54.     arr[i] = NULL;
  55.     return (arr);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement