Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define DIM 1000
  6.  
  7. int main()
  8. {
  9.    
  10.    
  11.     char * str = malloc(sizeof(char) * DIM);
  12.     fgets(str, 1000, stdin);
  13.    
  14.     char * last = (strchr(str, '\n'));
  15.     *last = 0;
  16.    
  17.     // printf("Hai inserito: %s\n", str);
  18.    
  19.     char * result = malloc(sizeof(char) * DIM);
  20.     char * result_p = result;
  21.    
  22.     char * token = strtok(str,"? .!,;");
  23.    
  24.    
  25.        
  26.        
  27.    
  28.    
  29.    
  30.     while(token) {
  31.        
  32.        
  33.         // printf("%s\n", token);
  34.        
  35.         const char * tmp = token;
  36.         while(*tmp++);
  37.        
  38.         int tokenLenght = strlen(token);
  39.        
  40.        
  41.         if(tmp != last + 1) {
  42.             if(!strstr(tmp, token)) {
  43.                 strncpy (result_p, token , tokenLenght);
  44.                 result_p += tokenLenght;
  45.                 *result_p = ' ';
  46.                 result_p++;
  47.             }
  48.         } else {
  49.             // l'ultima parola sicuramente non è stata inserita, nemmeno se era presente prima
  50.             // perché la logica del programma si basa sul guardare se una parola compare DOPO averla
  51.             // trovata
  52.             strncpy (result_p, token, tokenLenght);
  53.             result_p += tokenLenght;
  54.             *result_p = 0;
  55.         }
  56.    
  57.        
  58.         token = strtok(NULL,"? .!,;");
  59.     }
  60.    
  61.     printf("il risultato vale: %s", result);
  62.    
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement