Advertisement
eg0rmaffin

ft_strsplit LEAK

Sep 15th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include "libft.h"
  2.  
  3. int     ft_words_counter(char *s, char c)
  4. {
  5.     int words;
  6.     int run;
  7.  
  8.     words = 0;
  9.     run = 0;
  10.     while (s[run])
  11.     {
  12.         while (s[run] == c)
  13.             run++;
  14.         if (s[run + 1] == c)
  15.             words++;
  16.         run++;
  17.     }
  18.     return (words);
  19. }
  20.  
  21. size_t      ft_found_len(char *s, char c)
  22. {
  23.     size_t run;
  24.  
  25.     run = 0;
  26.     while (s[run] != c)
  27.         run++;
  28.     return (run);
  29. }
  30.  
  31.  
  32. char    **ft_strsplit(char const *s, char c)
  33. {
  34.     char        **arr;
  35.     size_t      run;
  36.     size_t      i;
  37.  
  38.     i = 0;
  39.     run = 0;
  40.     arr = (char**)ft_memalloc(sizeof(char*) * (ft_words_counter((char *)s, c) + 1));   //все ок точно ок
  41.     while (s[run])
  42.     {
  43.         while (s[run] == c)
  44.             run++;
  45.         if (s[run] != c)
  46.         {
  47.             arr[i] = ft_strsub(((char *)s + run), 0, ft_found_len((char *)s + run, c));
  48.             i++;
  49.             run = run + ft_found_len((char *)s + run, c) + 1;
  50.         }
  51.     }
  52.     return (arr);
  53. }
  54.  
  55.  
  56.  
  57.  
  58. int main() {
  59.     char *s = "      split       this for   me  !       ";
  60.     char **arr = ft_strsplit(s, ' ');
  61.  
  62.     for(int i = 0; i < ft_words_counter(s, ' '); i++)
  63.     {
  64.         free(arr[i]);
  65.     }
  66.     free(arr);
  67.  
  68.     printf("%s\n", arr[0]);
  69.     printf("%s\n", arr[1]);
  70.     printf("%s\n", arr[2]);
  71.     printf("%s\n", arr[3]);
  72.          printf("%s\n", arr[4]);
  73.          printf("%s\n", arr[5]);
  74.  
  75.          printf("%d\n", ft_words_counter(s, ' '));
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement