Advertisement
Guest User

white

a guest
Jul 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. include <stdlib.h>
  2.  
  3. int     count_words(char *str)
  4. {
  5.     int     i;
  6.     int     nbwords;
  7.  
  8.     nbwords = 0;
  9.     i = 0;
  10.     while (str[i])
  11.     {
  12.         while ((str[i] == ' ' || str[i] == '\t' || str[i] == '\n') &&
  13.                 str[i] != '\0')
  14.             i++;
  15.         if ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
  16.                 str[i] != '\0')
  17.         {
  18.             nbwords += 1;
  19.             while ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
  20.                     str[i] != '\0')
  21.                 i++;
  22.         }
  23.     }
  24.     return (nbwords);
  25. }
  26.  
  27. int     lettre(char *str, int nbword)
  28. {
  29.     int     i;
  30.     int     word;
  31.  
  32.     if (nbword == 0)
  33.         return (0);
  34.     word = 0;
  35.     i = 0;
  36.     while (str[i])
  37.     {
  38.         while ((str[i] == ' ' || str[i] == '\t' || str[i] == '\n') &&
  39.                 str[i] != '\0')
  40.             i++;
  41.         if ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
  42.                 str[i] != '\0')
  43.             word += 1;
  44.         if (word == nbword)
  45.             return (i);
  46.         else
  47.         {
  48.             while ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
  49.                     str[i] != '\0')
  50.                 i++;
  51.         }
  52.     }
  53.     return (0);
  54. }
  55.  
  56. int     lon(char *str, int lettre)
  57. {
  58.     int     i;
  59.  
  60.     i = 0;
  61.     while (str[i])
  62.         i++;
  63.     if (i == 0)
  64.         return (0);
  65.     i = lettre;
  66.     while ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
  67.             str[i] != '\0')
  68.         i++;
  69.     return (i - lettre);
  70. }
  71.  
  72. char    **norminet(char **tableau, int i, int j, char *str)
  73. {
  74.     if (count_words(str) == 0)
  75.         return (0);
  76.     if(!(tableau = malloc(sizeof(tableau) * count_words(str) + 1)))
  77. //  if (tableau == 0)
  78.         return (0);
  79.     while (i < count_words(str))
  80.     {
  81.         if (!(tableau[i] = malloc(sizeof(*tableau) * lon(str, lettre(str, i + 1)))))
  82. //      if (tableau[i] == 0)
  83.             return (0);
  84.         while (j < lon(str, lettre(str, i + 1)))
  85.         {
  86.             tableau[i][j] = str[lettre(str, i + 1) + j];
  87.             j++;
  88.         }
  89.         tableau[i][j] = '\0';
  90.         i++;
  91.         if (i == count_words(str))
  92.         {
  93.             tableau[i] = malloc(sizeof(tableau[i]));
  94.             tableau[i][j] = 0;
  95.         }
  96.         j = 0;
  97.     }
  98.     return (tableau);
  99. }
  100.  
  101. char    **ft_split_whitespaces(char *str)
  102. {
  103.     char    **tableau;
  104.     int     i;
  105.     int     j;
  106.  
  107.     tableau = NULL;
  108.     j = 0;
  109.     i = 0;
  110.     i = 0;
  111.     return (norminet(tableau, i, j, str));
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement