Advertisement
Guest User

per il mio amore

a guest
Feb 26th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. /* scrivere una funzione che, data una stringa s, restituisce una lista di stringhe i cui elementi
  2. sono le parole che compongono s.
  3. Nota: la funzione non deve modificare la stringa ricevuta in input.
  4. Esempio: Esame di Calcolatori -> <Esame>, <di>, <Calcolatori>*/
  5.  
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. #include<string.h>
  9.  
  10.  
  11. struct node{
  12. char s[20];
  13. struct node *next;
  14. };
  15.  
  16. void append(struct node **head, char *string){
  17.  
  18. struct node *current;
  19. struct node *new_node;
  20.  
  21. new_node=malloc(sizeof(struct node));
  22. if(new_node==NULL)
  23. {
  24. printf("Impossibile\n");
  25. exit(EXIT_FAILURE);
  26. }
  27.  
  28. strcpy(new_node->s,string);
  29. new_node->next=NULL;
  30.  
  31. if(*head == NULL)
  32. {
  33. *head=new_node;
  34. return;
  35. }
  36.  
  37. current=*head;
  38. while(current->next!=NULL)
  39. {
  40. current=current->next;
  41. }
  42. current->next=new_node;
  43.  
  44. }
  45.  
  46. void print_list(struct node *head){
  47. struct node *current;
  48. current = head;
  49. if(current == NULL)
  50. {
  51. printf("lista vuota\n");
  52. return;
  53. }
  54.  
  55. while(current!=NULL){
  56.  
  57. printf("%s --> ", current->s);
  58. current = current->next;
  59.  
  60. }
  61. printf("NULL\n");
  62. }
  63.  
  64.  
  65. struct node *func(struct node *head, char *str)
  66. {
  67. printf("secondo: %s\n", str);
  68. int i = 0;
  69. int j = 0;
  70.  
  71. while(str[j] != '\0'){
  72.  
  73. i = 0;
  74. char s[50] = " ";
  75.  
  76. while(str[j] != ' ' && str[j] != '\n' && str[j] != '\0'){
  77.  
  78. s[i]=str[j];
  79. i++;
  80. j++;
  81.  
  82. }
  83. append(&head,s);
  84. j++;
  85. }
  86.  
  87. return head;
  88.  
  89. }
  90.  
  91. int main()
  92. {
  93. struct node *head=NULL;
  94. char str[50]="Esame di Calcolatori";
  95.  
  96.  
  97. printf("primo: %s\n", str);
  98.  
  99.  
  100. head=func(head, str);
  101. print_list(head);
  102.  
  103. return 0;
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement