Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(void)
  6. {
  7.     char *a, *aux, *p, *s;
  8.     int c, n = 0, len, k = 0;
  9.  
  10.     a = (char *) malloc(1);
  11.  
  12.     if (!a)
  13.     {
  14.         printf("Nu pot aloca memorie.\n");
  15.         exit(EXIT_FAILURE);
  16.     }
  17.  
  18.     a[0] = 0;
  19.  
  20.     while ((c = getchar()) != EOF)
  21.     {
  22.         aux = (char *) realloc(a, n + 2);
  23.  
  24.         if (!aux)
  25.         {
  26.             free(a);
  27.             printf("Nu pot redimensiona blocul.\n");
  28.             exit(EXIT_FAILURE);
  29.         }
  30.         else
  31.             a = aux;
  32.  
  33.         a[n] = c;
  34.         a[n + 1] = 0;
  35.  
  36.         n ++;
  37.     }
  38.  
  39.     s = (char *) malloc(1);
  40.  
  41.     if (!s)
  42.     {
  43.         printf("Nu pot aloca memorie.\n");
  44.         free(a);
  45.         exit(EXIT_FAILURE);
  46.     }
  47.  
  48.     s[0] = 0;
  49.     p = strtok(a, "\t\n ");
  50.  
  51.     while (p != NULL)
  52.     {
  53.         if (k)
  54.         {
  55.             len = strlen(s);
  56.  
  57.             if (tolower(s[len - 2]) == tolower(p[0]) && tolower(s[len - 1]) == tolower(p[1]))
  58.             {
  59.                 aux = (char *) realloc(s, len + strlen(p) + 2);
  60.  
  61.                 if (!aux)
  62.                 {
  63.                     free(a);
  64.                     free(s);
  65.  
  66.                     printf("Nu pot redimensiona blocul.\n");
  67.                     exit(EXIT_FAILURE);
  68.                 }
  69.                 else
  70.                     s = aux;
  71.  
  72.                 strcat(s, "-");
  73.                 strcat(s, p);
  74.             }
  75.         }
  76.         else
  77.         {
  78.             aux = (char *) realloc(s, strlen(p) + 2);
  79.  
  80.             if (!aux)
  81.             {
  82.                 free(a);
  83.                 free(s);
  84.  
  85.                 printf("Nu pot redimensiona blocul.\n");
  86.                 exit(EXIT_FAILURE);
  87.             }
  88.             else
  89.                 s = aux;
  90.  
  91.             strcat(s, p);
  92.         }
  93.  
  94.         p = strtok(NULL, "\t\n ");
  95.         k ++;
  96.     }
  97.  
  98.     printf("%s", s);
  99.  
  100.     free(s);
  101.     free(a);
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement