Advertisement
Guest User

0208-splash

a guest
Jun 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. char *ft_strncpy(char *dest, char *src, unsigned int n)
  4. {
  5. unsigned int i;
  6.  
  7. i = 0;
  8. while (src[i] && i < n)
  9. {
  10. dest[i] = src[i];
  11. i++;
  12. }
  13. while (i < n)
  14. {
  15. dest[i] = '\0';
  16. i++;
  17. }
  18. dest[i] = '\0';
  19. return (dest);
  20. }
  21.  
  22. int is_charset(char c, char *charset)
  23. {
  24. int j;
  25.  
  26. j = 0;
  27. while (charset[j])
  28. {
  29. if (c == charset[j])
  30. return (1);
  31. j++;
  32. }
  33. return (0);
  34. }
  35.  
  36. int count_words(char *str, char *charset)
  37. {
  38. int count;
  39. int i;
  40.  
  41. i = 0;
  42. count = 0;
  43. while (str[i])
  44. {
  45. while (str[i] && is_charset(str[i], charset))
  46. i++;
  47. if (str[i] && !is_charset(str[i], charset))
  48. count++;
  49. while (str[i] && !is_charset(str[i], charset))
  50. i++;
  51. }
  52. return (count);
  53. }
  54.  
  55. char *ft_strdup(char *str, char *charset)
  56. {
  57. char *dest;
  58. int i;
  59. int j;
  60. int count;
  61.  
  62. i = 0;
  63. j = 0;
  64. count = 0;
  65. while (str[i])
  66. {
  67. if (is_charset(str[i], charset))
  68. break ;
  69. i++;
  70. }
  71. if (!(dest = (char*)malloc(sizeof(char) * i + 1)))
  72. return (NULL);
  73. dest = ft_strncpy(dest, str, i);
  74. return (dest);
  75. }
  76.  
  77. char **ft_split(char *str, char *charset)
  78. {
  79. char **tab;
  80. int i;
  81. int k;
  82.  
  83. if (!(tab = (char**)malloc(sizeof(char*) * (count_words(str, charset) + 1)))
  84. || !str)
  85. return (NULL);
  86. i = 0;
  87. k = 0;
  88. while (str[i] && k < count_words(str, charset))
  89. {
  90. while (is_charset(str[i], charset))
  91. i++;
  92. if (str[i] && !is_charset(str[i], charset))
  93. {
  94. tab[k] = ft_strdup(str + i, charset);
  95. k++;
  96. }
  97. while (str[i] && !is_charset(str[i], charset))
  98. i++;
  99. }
  100. tab[k] = 0;
  101. return (tab);
  102. }
  103.  
  104. #include <stdio.h>
  105.  
  106. int main(int ac, char **av)
  107. {
  108. int i;
  109. char **split;
  110.  
  111. i = 0;
  112. if (ac != 3)
  113. return (0);
  114. split = ft_split(av[1], av[2]);
  115. while (split[i])
  116. {
  117. printf("%s\n", split[i]);
  118. i++;
  119. }
  120. return (0);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement