Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(void)
- {
- char *a, *aux, *p, *s;
- int c, n = 0, len, k = 0;
- a = (char *) malloc(1);
- if (!a)
- {
- printf("Nu pot aloca memorie.\n");
- exit(EXIT_FAILURE);
- }
- a[0] = 0;
- while ((c = getchar()) != EOF)
- {
- aux = (char *) realloc(a, n + 2);
- if (!aux)
- {
- free(a);
- printf("Nu pot redimensiona blocul.\n");
- exit(EXIT_FAILURE);
- }
- else
- a = aux;
- a[n] = c;
- a[n + 1] = 0;
- n ++;
- }
- s = (char *) malloc(1);
- if (!s)
- {
- printf("Nu pot aloca memorie.\n");
- free(a);
- exit(EXIT_FAILURE);
- }
- s[0] = 0;
- p = strtok(a, "\t\n ");
- while (p != NULL)
- {
- if (k)
- {
- len = strlen(s);
- if (tolower(s[len - 2]) == tolower(p[0]) && tolower(s[len - 1]) == tolower(p[1]))
- {
- aux = (char *) realloc(s, len + strlen(p) + 2);
- if (!aux)
- {
- free(a);
- free(s);
- printf("Nu pot redimensiona blocul.\n");
- exit(EXIT_FAILURE);
- }
- else
- s = aux;
- strcat(s, "-");
- strcat(s, p);
- }
- }
- else
- {
- aux = (char *) realloc(s, strlen(p) + 2);
- if (!aux)
- {
- free(a);
- free(s);
- printf("Nu pot redimensiona blocul.\n");
- exit(EXIT_FAILURE);
- }
- else
- s = aux;
- strcat(s, p);
- }
- p = strtok(NULL, "\t\n ");
- k ++;
- }
- printf("%s", s);
- free(s);
- free(a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement