Guest User

Untitled

a guest
Jul 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int ft_isword(char *str, int i)
  5. {
  6. return (str[i] && ((str[i] != ' ') && (str[i] != '\t')
  7. && (str[i] != '\n')));
  8. }
  9.  
  10. int ft_count(char *str)
  11. {
  12. int i;
  13. int nb_word;
  14.  
  15. i = 0;
  16. nb_word = 0;
  17. while (str[i])
  18. {
  19. if (ft_isword(str, i))
  20. {
  21. nb_word++;
  22. while (ft_isword(str, i))
  23. i++;
  24. }
  25. else
  26. i++;
  27. }
  28. return (nb_word);
  29. }
  30.  
  31. int ft_wordlen(char *str)
  32. {
  33. int i;
  34.  
  35. i = 0;
  36. while (ft_isword(str, i))
  37. i++;
  38. return (i);
  39. }
  40.  
  41. char **ft_split_whitespaces(char *str)
  42. {
  43. int nb_word;
  44. int i;
  45. char **dest;
  46. int k;
  47. int j;
  48.  
  49. i = 0;
  50. j = 0;
  51. k = 0;
  52. nb_word = ft_count(str);
  53. dest = malloc(sizeof(char*) * (nb_word + 1));
  54. dest[nb_word] = 0;
  55. while (str[i] != 0)
  56. {
  57. if (ft_isword(str, i))
  58. {
  59. dest[k] = malloc(sizeof(char) * (ft_wordlen(str + i) + 1));
  60. j = 0;
  61. while (ft_isword(str, i))
  62. {
  63. dest[k][j] = str[i];
  64. i++;
  65. j++;
  66. }
  67. dest[k][j] = '\0';
  68. k++;
  69. }
  70. else
  71. i++;
  72. }
  73. return (dest);
  74. }
  75.  
  76. void ft_xd(char **w)
  77. {
  78. for (int i = 0; w[i] != NULL; i++)
  79. {
  80. printf("%d: %s\n", i, w[i]);
  81. }
  82. }
  83.  
  84. int main(int ac, char **av)
  85. {
  86. char **mdr;
  87.  
  88. mdr = ft_split_whitespaces(av[1]);
  89. ft_xd(mdr);
  90. return (0);
  91. }
Add Comment
Please, Sign In to add comment