fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. struct Word
  7. {
  8. char* data;
  9. struct Word* next;
  10. };
  11.  
  12. struct Word* ptr = NULL;
  13.  
  14.  
  15. void insert(char c)
  16. {
  17. struct Word* temp = (struct Word*)malloc(sizeof(struct Word));
  18.  
  19. temp->data = c;
  20. temp->next = NULL;
  21.  
  22. if (ptr) {
  23. struct Word* temp1 = ptr;
  24.  
  25. while(temp1->next != NULL) {
  26. temp1 = temp1->next;
  27. }
  28.  
  29. temp1->next = temp;
  30.  
  31. } else {
  32. ptr = temp;
  33. }
  34.  
  35. }
  36.  
  37. void freeData(struct Word* head)
  38. {
  39. struct Word* tmp;
  40.  
  41. while (head != NULL)
  42. {
  43. tmp = head;
  44. head = head->next;
  45. free(tmp);
  46. }
  47.  
  48. }
  49.  
  50. void print() {
  51.  
  52. struct Word *temp;
  53. temp = ptr;
  54. char c;
  55.  
  56. while(temp != NULL) {
  57.  
  58. if (temp->data == ',') {
  59. printf("\n");
  60. temp = temp->next;
  61. } else {
  62. printf("%c", temp->data);
  63. temp = temp->next;
  64. }
  65.  
  66. }
  67.  
  68.  
  69. printf("\n");
  70.  
  71. }
  72.  
  73. int main(int argc, char *argv[])
  74. {
  75. int c;
  76.  
  77. printf("enter a string\n");
  78. while (((c=getchar())!=EOF) && c!='\n') {
  79. insert((char)c);
  80. }
  81.  
  82. print(); /*print the list*/
  83. freeData(ptr);
  84. return 0;
  85. }
Success #stdin #stdout 0s 10304KB
stdin
hello,world,how,are,you
stdout
enter a string
hello
world
how
are
you