IvoSilva

[PROG1] Ficha 12 | Exercício 3

Jan 16th, 2012
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. void main()
  5. {
  6.     char **words = NULL, temp[80] = "\0" ;
  7.     int  n = 0 , j ;
  8.     printf ("Insira strings (para acabar insira 0):\n") ;
  9.     while (strcmp(temp , "0\n") != 0)
  10.     {
  11.         fgets (temp , 80 , stdin) ;
  12.         if (strcmp(temp , "0\n") == 0 ) break ;
  13.         for (j = 0 ; j < n ; j++) if (strcmp(temp , words[j]) == 0) break ;
  14.         if (j >= n)
  15.         {
  16.             words = (char **)realloc (words,(n + 1) * sizeof(char*)) ;
  17.             words[n] = (char *)malloc ((strlen(temp) + 1) * sizeof(char)) ;
  18.             strcpy ( words[n++], temp ) ;
  19.         }
  20.     }
  21.     for (j = 0 ; j < n ; j++) printf ("%s" , words[j]) ;
  22.     free (words) ;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment