Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- int ft_isword(char *str, int i)
- {
- return (str[i] && ((str[i] != ' ') && (str[i] != '\t')
- && (str[i] != '\n')));
- }
- int ft_count(char *str)
- {
- int i;
- int nb_word;
- i = 0;
- nb_word = 0;
- while (str[i])
- {
- if (ft_isword(str, i))
- {
- nb_word++;
- while (ft_isword(str, i))
- i++;
- }
- else
- i++;
- }
- return (nb_word);
- }
- int ft_wordlen(char *str)
- {
- int i;
- i = 0;
- while (ft_isword(str, i))
- i++;
- return (i);
- }
- char **ft_split_whitespaces(char *str)
- {
- int nb_word;
- int i;
- char **dest;
- int k;
- int j;
- i = 0;
- j = 0;
- k = 0;
- nb_word = ft_count(str);
- dest = malloc(sizeof(char*) * (nb_word + 1));
- dest[nb_word] = 0;
- while (str[i] != 0)
- {
- if (ft_isword(str, i))
- {
- dest[k] = malloc(sizeof(char) * (ft_wordlen(str + i) + 1));
- j = 0;
- while (ft_isword(str, i))
- {
- dest[k][j] = str[i];
- i++;
- j++;
- }
- dest[k][j] = '\0';
- k++;
- }
- else
- i++;
- }
- return (dest);
- }
- void ft_xd(char **w)
- {
- for (int i = 0; w[i] != NULL; i++)
- {
- printf("%d: %s\n", i, w[i]);
- }
- }
- int main(int ac, char **av)
- {
- char **mdr;
- mdr = ft_split_whitespaces(av[1]);
- ft_xd(mdr);
- return (0);
- }
Add Comment
Please, Sign In to add comment