[ create a new paste ] login | about

Link: http://codepad.org/T3oBbkBj    [ raw code | output | fork ]

ninwa - C, pasted on Oct 16:
/* Slide, takes an array and allows 
   you to move, with wrap-around, all characters
   to the right. */

char _slide_one(char* arr, int sz) {
  static char front = '\0';

  char c = arr[--sz];

  if(front == '\0')
    front = c;

  if(sz == 0) {
    arr[sz] = front;
    front = '\0';
  }
  else 
    arr[sz] = _slide_one(arr, sz);

  return c;
}

void slide(char* arr, int n) {
  int sz = strlen(arr);

  if(n >= sz) n = sz % n;
  if(sz <= 1 || n == 0) return;

  while(n-- != 0)
    _slide_one(arr, sz);
}

int main(int argc, char* argv[]) {
  char s[] = "abcd";
  slide(s,2);
  printf(s);

  return 0;
}


Output:
1
cdab


Create a new paste based on this one


Comments: