Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include "ft_stock_str.h"
  2. #include <stdlib.h>
  3.  
  4. int ft_strlen(char *str)
  5. {
  6. int i;
  7.  
  8. i = 0;
  9. while (str[i])
  10. i++;
  11. return (i);
  12. }
  13.  
  14. char *ft_strdup(char *str)
  15. {
  16. int i;
  17. char *p;
  18.  
  19. i = 0;
  20. p = malloc(sizeof(char) * ft_strlen(str) + 1);
  21. if (p == NULL)
  22. return (p);
  23. while (str[i])
  24. {
  25. p[i] = str[i];
  26. i++;
  27. }
  28. p[i] = '\0';
  29. return (p);
  30. }
  31.  
  32. struct s_stock_str *ft_strs_to_tab(int ac, char **av)
  33. {
  34. int i;
  35. t_stock_str *tab;
  36.  
  37. i = 1;
  38. tab = malloc(sizeof(t_stock_str) * ac + 1);
  39. if (tab == NULL)
  40. return (tab);
  41. while (i < ac)
  42. {
  43. tab[i].size = ft_strlen(av[i]);
  44. tab[i].str = av[i];
  45. tab[i].copy = ft_strdup(av[i]);
  46. i++;
  47. }
  48. tab[i].str = 0;
  49. return (tab);
  50. }
  51.  
  52. ----------------------------------------------------------------------------------------------------
  53.  
  54. #ifndef FT_STOCK_STR_H
  55. # define FT_STOCK_STR_H
  56.  
  57. typedef struct s_stock_str
  58. {
  59. int size;
  60. char *str;
  61. char *copy;
  62. } t_stock_str;
  63. #endif
  64.  
  65. --------------------------------------------------------------------------------------------------------
  66.  
  67. #include <unistd.h>
  68. #include "ft_stock_str.h"
  69.  
  70. void ft_putchar(char c)
  71. {
  72. write(1, &c, 1);
  73. }
  74.  
  75. char *ft_putstr(char *str)
  76. {
  77. int i;
  78.  
  79. i = 0;
  80. while(str[i])
  81. {
  82. ft_putchar(str[i]);
  83. i++;
  84. }
  85. return (str);
  86. }
  87.  
  88. void ft_putnbr(int nb)
  89. {
  90. char ent;
  91.  
  92. if (nb == -2147483648)
  93. {
  94. write(1, "-2147483648", 11);
  95. return ;
  96. }
  97. if (nb < 0)
  98. {
  99. nb = -nb;
  100. ft_putchar('-');
  101. }
  102. if (nb >= 0 && nb <= 9)
  103. {
  104. ent = nb + '0';
  105. ft_putchar(ent);
  106. }
  107. else
  108. {
  109. ft_putnbr(nb / 10);
  110. ft_putchar(nb % 10 + '0');
  111. }
  112. }
  113.  
  114. void ft_show_tab(struct s_stock_str *par)
  115. {
  116. int i;
  117.  
  118. i = 0;
  119. while (par[i].str != 0)
  120. {
  121. ft_putstr((par[i]).str);
  122. ft_putchar('\n');
  123. ft_putnbr((par[i]).size);
  124. ft_putchar('\n');
  125. ft_putstr((par[i]).copy);
  126. ft_putchar('\n');
  127. i++;
  128. }
  129. }
  130.  
  131. int main(int argc, char **argv)
  132. {
  133. ft_show_tab(ft_strs_to_tab(argc, argv));
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement