Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* scrivere una funzione che, data una stringa s, restituisce una lista di stringhe i cui elementi
- sono le parole che compongono s.
- Nota: la funzione non deve modificare la stringa ricevuta in input.
- Esempio: Esame di Calcolatori -> <Esame>, <di>, <Calcolatori>*/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct node{
- char s[20];
- struct node *next;
- };
- void append(struct node **head, char *string){
- struct node *current;
- struct node *new_node;
- new_node=malloc(sizeof(struct node));
- if(new_node==NULL)
- {
- printf("Impossibile\n");
- exit(EXIT_FAILURE);
- }
- strcpy(new_node->s,string);
- new_node->next=NULL;
- if(*head == NULL)
- {
- *head=new_node;
- return;
- }
- current=*head;
- while(current->next!=NULL)
- {
- current=current->next;
- }
- current->next=new_node;
- }
- void print_list(struct node *head){
- struct node *current;
- current = head;
- if(current == NULL)
- {
- printf("lista vuota\n");
- return;
- }
- while(current!=NULL){
- printf("%s --> ", current->s);
- current = current->next;
- }
- printf("NULL\n");
- }
- struct node *func(struct node *head, char *str)
- {
- printf("secondo: %s\n", str);
- int i = 0;
- int j = 0;
- while(str[j] != '\0'){
- i = 0;
- char s[50] = " ";
- while(str[j] != ' ' && str[j] != '\n' && str[j] != '\0'){
- s[i]=str[j];
- i++;
- j++;
- }
- append(&head,s);
- j++;
- }
- return head;
- }
- int main()
- {
- struct node *head=NULL;
- char str[50]="Esame di Calcolatori";
- printf("primo: %s\n", str);
- head=func(head, str);
- print_list(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement