Advertisement
KgCro

Rereverse - G3 grupa 2 Kolokvij 2018/2019

Jun 3rd, 2020
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. #ifndef DEBUG
  7. #define DEBUG(...) printf(__VA_ARGS__)
  8. #endif
  9.  
  10. void swap(char *a, char *b){
  11.   char tmp = *a;
  12.   *a = *b;
  13.   *b = tmp;
  14. }
  15.  
  16. void reverse(char *s, char *l, char *r) {
  17.   int n = *r - *l;
  18.   // zasto izbacuje negativan n kad reversam od 0 do kraja stringa ????
  19.  
  20.   printf("\n n = %d\n", n);
  21.  
  22.   int n2 = 0;
  23.   while(*s){
  24.     n2++;
  25.     s++;
  26.   }
  27.   //printf("\n n2 = %d\n", n2);
  28.  
  29.   if (n2 < 4 || n == 1) // ako su 2 ili 3 elementa
  30.   {
  31.     swap(r,l);
  32.     printf("\n--Usao sam u 1. if--\n");
  33.   }
  34.  
  35.   if (n%2==0 && n != 2)
  36.   {
  37.     for (int i = 0; i <= n/2; ++i)
  38.     {
  39.       swap(l+i,r-i);
  40.       printf("\n--Usao sam u 2. if--\n");
  41.     }
  42.   }else if (n%2==1 && n != 1)
  43.   {
  44.     for (int i = 0; i <= n/2; ++i)
  45.     {
  46.       swap(l+i,r-i);
  47.       printf("\n--Usao sam u 3. if--\n");
  48.     }
  49.   }
  50. }
  51.  
  52. int main() {
  53.   int n, q;
  54.   char *s;
  55.   scanf("%d%d", &n, &q);
  56.   s = malloc((n+1) * sizeof(char));
  57.   scanf(" %s", s);
  58.   for (int i = 0; i < q; ++i) {
  59.     int l, r;
  60.     scanf("%d%d", &l, &r);
  61.     reverse(s, s+l, s+r);
  62.   }
  63.   printf("%s\n", s);
  64.   free(s);
  65.   return 0;
  66. }
  67. /*
  68.   // nesluzbeni testcase koji zadaje muke
  69.  
  70. 4 1
  71. dcba
  72. 0 3
  73.  
  74. n = -3 ovdje zasto ??????
  75.  
  76. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement