Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include <stdlib.h>
- int count_words(char *str)
- {
- int i;
- int nbwords;
- nbwords = 0;
- i = 0;
- while (str[i])
- {
- while ((str[i] == ' ' || str[i] == '\t' || str[i] == '\n') &&
- str[i] != '\0')
- i++;
- if ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
- str[i] != '\0')
- {
- nbwords += 1;
- while ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
- str[i] != '\0')
- i++;
- }
- }
- return (nbwords);
- }
- int lettre(char *str, int nbword)
- {
- int i;
- int word;
- if (nbword == 0)
- return (0);
- word = 0;
- i = 0;
- while (str[i])
- {
- while ((str[i] == ' ' || str[i] == '\t' || str[i] == '\n') &&
- str[i] != '\0')
- i++;
- if ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
- str[i] != '\0')
- word += 1;
- if (word == nbword)
- return (i);
- else
- {
- while ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
- str[i] != '\0')
- i++;
- }
- }
- return (0);
- }
- int lon(char *str, int lettre)
- {
- int i;
- i = 0;
- while (str[i])
- i++;
- if (i == 0)
- return (0);
- i = lettre;
- while ((str[i] != ' ' && str[i] != '\t' && str[i] != '\n') &&
- str[i] != '\0')
- i++;
- return (i - lettre);
- }
- char **norminet(char **tableau, int i, int j, char *str)
- {
- if (count_words(str) == 0)
- return (0);
- if(!(tableau = malloc(sizeof(tableau) * count_words(str) + 1)))
- // if (tableau == 0)
- return (0);
- while (i < count_words(str))
- {
- if (!(tableau[i] = malloc(sizeof(*tableau) * lon(str, lettre(str, i + 1)))))
- // if (tableau[i] == 0)
- return (0);
- while (j < lon(str, lettre(str, i + 1)))
- {
- tableau[i][j] = str[lettre(str, i + 1) + j];
- j++;
- }
- tableau[i][j] = '\0';
- i++;
- if (i == count_words(str))
- {
- tableau[i] = malloc(sizeof(tableau[i]));
- tableau[i][j] = 0;
- }
- j = 0;
- }
- return (tableau);
- }
- char **ft_split_whitespaces(char *str)
- {
- char **tableau;
- int i;
- int j;
- tableau = NULL;
- j = 0;
- i = 0;
- i = 0;
- return (norminet(tableau, i, j, str));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement