Advertisement
KgCro

Strip string - Kolokvij prošle godine

Jun 2nd, 2020
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifndef DEBUG
  5. #define DEBUG(...) printf(__VA_ARGS__)
  6. #endif
  7.  
  8. void strip(char **first, char **last, char *chars) {
  9.   //int len = *last - *first;
  10.  
  11.   char *tmp = chars;
  12.  
  13.   while(*tmp){
  14.     char *tmp2 = chars;
  15.     while(*tmp2){
  16.       if (**first == *tmp2 && **last == *tmp2)
  17.       {
  18.         *first += 1;
  19.         *last -= 1;
  20.         //printf("test\n");
  21.       }
  22.       if (**first == *tmp2)
  23.       {
  24.         *first += 1;
  25.       }
  26.       if (**last == *tmp2){
  27.         *last -= 1;
  28.       }
  29.       tmp2++;
  30.     }
  31.     tmp++;
  32.   }
  33. }
  34.  
  35. int main() {
  36.   int n, q;
  37.   char *s, *chars;
  38.   scanf("%d%d", &n, &q);
  39.   s = malloc((n+1) * sizeof(char));
  40.   chars = malloc(26 * sizeof(char));
  41.   scanf("%s", s);
  42.   for (int i = 0; i < q; ++i) {
  43.     char *first = s, *last = s+n;
  44.     scanf("%s", chars);
  45.     strip(&first, &last, chars);
  46.     // ispisuje znakovni niz duljine last-first počevši od pokazivača first
  47.     printf("%d %.*s\n", (int)(last-first), (int)(last-first), first);
  48.   }
  49.   free(s);
  50.   free(chars);
  51.   return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement