Advertisement
STANAANDREY

pheasant pc lab

Oct 19th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #define NMAX 1000
  6. #define NULL_CHECK(p) if (p == NULL) return p;
  7.  
  8. char *pheasant(char *s) {
  9.   char *res = NULL;
  10.   char *token = strtok(s, " ");
  11.   int n = strlen(token) + 2;
  12.   res = calloc(n, sizeof(char));
  13.   strcpy(res, token);
  14.   res[n-2] = '-';
  15.   NULL_CHECK(res);
  16.   token = strtok(NULL, " ");
  17.   while (token) {
  18.     if (tolower(token[0]) == tolower(res[n - 4]) && tolower(token[1]) == tolower(res[n - 3])) {
  19.       n += strlen(token) + 1;
  20.       res = realloc(res, sizeof(char) * n);
  21.       NULL_CHECK(res);
  22.       strcat(res, token);
  23.       strcat(res, "-");
  24.     }
  25.     token = strtok(NULL, " ");
  26.   }
  27.   res[n - 2] = 0;
  28.   return res;
  29. }
  30.  
  31. int main(void) {
  32.   static char s[NMAX] = "Fazan antic";
  33.   char *res = pheasant(s);
  34.   if (res == NULL) {
  35.     fprintf(stderr, "alloc error!\n");
  36.     exit(-1);
  37.   }
  38.   puts(res);
  39.   free(res);
  40.   return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement