How to perform a circular shift of the elements of an array to the left in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

void circularLeftShift(int arr[], int size) {
    int temp = arr[0];
    for (int i = 0; i < size - 1; i++) {
        arr[i] = arr[i + 1];
    }
    arr[size - 1] = temp;
}

int main() {
    unsigned int arr[]= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    circularLeftShift(arr, size);
    
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
  
    return 0;
}

 
         
/*
run:
      
1 2 3 4 5 6 7 8 9 0 
     
*/

 



answered Jul 5, 2024 by avibootz
...