fork(5) download
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3.  
  4. void my_putchar(char c)
  5. {
  6. write(1, &c, 1);
  7. }
  8.  
  9. int my_putstr(char const *str)
  10. {
  11. int c = 0;
  12.  
  13. while (str[c] != '\0') {
  14. my_putchar(str[c]);
  15. c++;
  16. }
  17. return (c);
  18. }
  19.  
  20. int is_char(char c)
  21. {
  22. if ((c >= '0' && c <= '9')||(c >= 'A' && c <= 'Z')||(c >= 'a' && c <= 'z')) {
  23. return (1);
  24. } else {
  25. return (2);
  26. }
  27. }
  28.  
  29. int my_wordlen(char const *str)
  30. {
  31. int c = 0;
  32.  
  33. while (is_char(str[c]) == 1) {
  34. c++;
  35. }
  36.  
  37. int count_word(char const *str)
  38. {
  39. int a = 0;
  40. int b = 0;
  41.  
  42. while (str[a] != '\0') {
  43. if (is_char(str[a]) == 1) {
  44. a++;
  45. } else {
  46. a++;
  47. b++;
  48. }
  49. }
  50. return (b);
  51. }
  52.  
  53. char *my_strdupword(char const *src)
  54. {
  55. char *dest;
  56. int i;
  57.  
  58. dest = malloc(sizeof(char) * (my_wordlen(src) + 1));
  59. for (i = 0; is_char(src[i]) == 1; i++) {
  60. dest[i] = src[i];
  61. }
  62. dest[i] = '\n';
  63. return (dest);
  64. }
  65.  
  66. char **my_str_to_word_array(char const *str)
  67. {
  68. int j = 0;
  69. int l = 0;
  70. int k = 0;
  71. int nb_word;
  72. char **tab;
  73. char *case_tab;
  74.  
  75. nb_word = count_word(str);
  76. tab = malloc(sizeof(char*) * (nb_word + 1));
  77. while (str[l] != '\0') {
  78. while (j < nb_word) {
  79. k++;
  80. l++;
  81. if (is_char(str[k]) == 1) {
  82. my_strdupword(str);
  83. l++;
  84. k++;
  85. case_tab = my_strdupword(str);
  86. } else if (is_char(str[l]) == 2) {
  87. my_putchar('\n');
  88. l++;
  89. }
  90. l++;
  91. j++;
  92. }
  93. *tab = case_tab;
  94. }
  95. return (tab);
  96. }
  97.  
  98. int main()
  99. {
  100. char *str_to_array = "bonjour, jespere,que ca va";
  101. my_str_to_word_array(str_to_array);
  102. free(str_to_array);
  103. return 0;
  104. }
  105.  
  106.  
  107.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘my_wordlen’:
prog.c:98:5: warning: ‘main’ is normally a non-static function [-Wmain]
 int main()
     ^~~~
prog.c:104:1: error: expected declaration or statement at end of input
 }
 ^
prog.c: In function ‘my_putchar’:
prog.c:6:5: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
     write(1, &c, 1);
     ^~~~~~~~~~~~~~~
prog.c: In function ‘my_wordlen’:
prog.c:104:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
At top level:
prog.c:98:5: warning: ‘main’ defined but not used [-Wunused-function]
 int main()
     ^~~~
stdout
Standard output is empty