Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. int ft_count(char *str)
  4. {
  5.     int i;
  6.     int nb_word;
  7.  
  8.     i = 0;
  9.     nb_word = 0;
  10.     while (str[i])
  11.     {
  12.         if (str[i] && !(str[i] == ' ' || str[i] == '\t' || str[i] == '\n'))
  13.         {
  14.             nb_word++;
  15.             while (str[i] && !(str[i] == ' ' || str[i] == '\t' || str[i] == '\n'))
  16.                 i++;
  17.         }
  18.         else
  19.             i++;
  20.     }
  21.     return (nb_word);
  22. }
  23.  
  24. int ft_wordlen(char *str)
  25. {
  26.     int i;
  27.  
  28.     i = 0;
  29.     while (str[i] && !(str[i] == ' ' || str[i] == '\t' || str[i] == '\n'))
  30.         i++;
  31.     return (i);
  32. }
  33.  
  34. char **ft_split_whitespaces(char *str)
  35. {
  36.     int nb_word;
  37.     int i;
  38.     char **dest;
  39.     int k;
  40.     int j;
  41.  
  42.     i = 0;
  43.     j = 0;
  44.     k = 0;
  45.     nb_word = ft_count(str);
  46.                    printf("%i", nb_word);
  47.     dest = (char**)malloc(sizeof(char*) * (nb_word + 1));
  48.     dest[nb_word] = 0;
  49.  
  50.     while (dest[k] != 0)
  51.     {
  52.         if (str[i] && !(str[i] == ' ' || str[i] == '\t' || str[i] == '\n'))
  53.         {
  54.             dest[k] = (char*)malloc(sizeof(char) * (ft_wordlen(&str[i]) + 1));
  55.             j = 0;
  56.                        printf("-%i-", i);
  57.             while (str[i] && !(str[i] == ' ' || str[i] == '\t' || str[i] == '\n'))
  58.                 {
  59.                     dest[k][j] = str[i];
  60.                     i++;
  61.                     j++;
  62.                 }
  63.             dest[k][j] = '\0';
  64.             k++;
  65.         }
  66.         else
  67.             i++;
  68.     }
  69.     return (dest);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement