Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_split.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2019/11/11 23:11:24 by marvin #+# #+# */
  9. /* Updated: 2019/11/11 23:11:24 by marvin ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15.  
  16. void ft_free(char **dest, int wcount)
  17. {
  18. while (wcount >= 0)
  19. {
  20. free(dest[wcount]);
  21. wcount--;
  22. }
  23. free(dest);
  24. }
  25.  
  26. int ft_countletter(char *s, char c, int i)
  27. {
  28. int j;
  29.  
  30. j = 0;
  31. while (s[i + j] != c)
  32. j++;
  33. return (j);
  34. }
  35.  
  36. char **ft_fillword(char *s, char c, char **dest)
  37. {
  38. int i;
  39. int letter;
  40. int wcount;
  41.  
  42. i = 0;
  43. wcount = 0;
  44. while (s[i])
  45. {
  46. while (s[i] == c)
  47. i++;
  48. if (s[i] == 0)
  49. return (dest);
  50. if (!(dest[wcount] = malloc((ft_countletter(s, c, i) + 1) * sizeof(char))))
  51. ft_free(dest, wcount);
  52. letter = 0;
  53. while (s[i] != c && s[i] != 0)
  54. {
  55. dest[wcount][letter] = s[i];
  56. i++;
  57. letter++;
  58. }
  59. dest[wcount][letter] = 0;
  60. wcount++;
  61. }
  62. dest[wcount] = 0;
  63. return (dest);
  64. }
  65.  
  66. int ft_countworld(char *s, char c)
  67. {
  68. int i;
  69. int wcount;
  70.  
  71. i = 0;
  72. wcount = 0;
  73. while (s[i])
  74. {
  75. while (s[i] == c)
  76. i++;
  77. if (s[i] == 0)
  78. return (wcount);
  79. wcount++;
  80. while (s[i] != c && s[i] != 0)
  81. i++;
  82. }
  83. return (wcount);
  84. }
  85.  
  86. char **ft_split(char const *s, char c)
  87. {
  88. char **dest;
  89. int w_count;
  90.  
  91. w_count = ft_countworld((char *)s, c);
  92. if (!w_count)
  93. return (0);
  94. if (!(dest = malloc((w_count + 1) * sizeof(char *))))
  95. return (0);
  96. dest = ft_fillword((char *)s, c, dest);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement