Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main()
  5. {
  6.     char **groups, word[30], **store, n = 0, k = 0;
  7.     store = malloc(1*sizeof(char *));
  8.     if (store == NULL) exit(1);
  9.     for (;;) {
  10.         printf("Enter word: ");
  11.         if (scanf("%s", word) == EOF) break;
  12.         store[n] = malloc((strlen(word)+1)*sizeof(char));
  13.         if (store[n] == NULL) exit(1);
  14.         strcpy(store[n], word);
  15.         n++;
  16.         store = realloc(store, (n+1)*sizeof(char *));
  17.         if (store == NULL) exit(1);
  18.     }
  19.     for (int i=0; i<n; i++) {
  20.         printf("%s ", store[i]);
  21.     }
  22.     printf("\n");
  23.     for (int i=0; i<n; i++) {
  24.         for (int j=0; j<strlen(store[i])-1; j++) {
  25.             for (int l=(j+1); l<strlen(store[i]); l++) {
  26.                 if (store[i][j] > store[i][l]) {
  27.                     char aux = store[i][j];
  28.                     store[i][j] = store[i][l];
  29.                     store[i][l] = aux;
  30.                 }
  31.             }
  32.         }
  33.     }
  34.     printf("Sorted list: ");
  35.     for (int i=0; i<n; i++) {
  36.         printf("%s ", store[i]);
  37.     }
  38.  
  39.     char **dupl = malloc(n*sizeof(char *));
  40.  
  41.     for (int ii = 0; ii < n; ii++) {
  42.       dupl[ii] = strdup(store[ii]);
  43.     }
  44.  
  45.     printf("\n");
  46.     for (int i=0; i<n; i++) {
  47.         for (int j=0; j<strlen(dupl[i]); j++) {
  48.             if (dupl[i][j] == dupl[i][j+1]) {
  49.                 for (int l=j; l<strlen(dupl[i])-1; l++) {
  50.                     dupl[i][l] = dupl[i][l+1];
  51.                 }
  52.                 dupl[i][strlen(dupl[i]) - 1] = '\0';
  53.                 j--;
  54.             }
  55.         }
  56.     }
  57.     printf("Deduplicated list: ");
  58.     for (int i=0; i<n; i++) {
  59.         printf("%s ", dupl[i]);
  60.     }
  61.     printf("\n");
  62.     free(dupl);
  63.     free(store);
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement