C Program to Reverse an Array
Reversing an array is one of the most important and commonly asked programming problems in C. It helps beginners understand arrays, indexing, loops, swapping techniques, recursion, and pointer manipulation.
In this tutorial, we will explore multiple approaches to reverse an array including using a temporary variable, without using a temporary array, using recursion, and using pointers. We will also discuss common mistakes and practical applications.
1. Understanding the Problem
Given an array of elements, the task is to reverse the order of elements so that the first element becomes the last, the second becomes the second last, and so on.
Original Array: 1 2 3 4 5 Reversed Array: 5 4 3 2 1
If the array contains n elements, we swap the first element with the last, second with second last, and continue this process until the middle of the array.
2. Algorithm to Reverse an Array (Using Swapping)
Step-by-step algorithm:
1. Start.
2. Read the number of elements n.
3. Input n elements into the array.
4. Initialize two variables: start = 0 and end = n - 1.
5. While start < end:
- Swap array[start] and array[end].
- Increment start.
- Decrement end.
6. Print the reversed array.
7. Stop.
3. C Program to Reverse an Array Using Loop
This program reverses the array by swapping elements from both ends.
#include <stdio.h>
int main() {
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int start = 0, end = n - 1, temp;
while(start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
printf("Reversed array:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Input: Enter number of elements: 5 1 2 3 4 5 Output: Reversed array: 5 4 3 2 1
4. Reverse an Array Using Another Array
Another approach is to create a second array and copy elements in reverse order.
#include <stdio.h>
int main() {
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n], rev[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n; i++) {
rev[i] = arr[n - 1 - i];
}
printf("Reversed array:\n");
for(i = 0; i < n; i++) {
printf("%d ", rev[i]);
}
return 0;
}
5. Reverse an Array Using Recursion
Recursion can also be used to reverse an array. In recursion, the function calls itself until the base condition is met.
#include <stdio.h>
void reverse(int arr[], int start, int end) {
if(start >= end)
return;
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverse(arr, start + 1, end - 1);
}
int main() {
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
reverse(arr, 0, n - 1);
printf("Reversed array:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
6. Reverse an Array Using Pointers
Pointers provide an efficient way to manipulate arrays. Since array names act as pointers, we can use pointer arithmetic to reverse the array.
#include <stdio.h>
int main() {
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int *start = arr;
int *end = arr + n - 1;
int temp;
while(start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
printf("Reversed array:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
7. Time and Space Complexity
Time Complexity: O(n) because each element is processed once.
Space Complexity:
- O(1) for swapping method (in-place reversal).
- O(n) for extra array method.
The in-place method is preferred in interviews and competitive programming.
8. Common Mistakes
1. Incorrect loop condition (using <= instead of <).
2. Forgetting to decrement end pointer.
3. Not handling empty arrays.
4. Using uninitialized variables.
while(start <= end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
// Missing end-- causes incorrect behavior
}
9. Applications of Array Reversal
1. Used in data structure problems.
2. Helpful in string reversal logic.
3. Used in algorithm optimization techniques.
4. Required in interview coding questions.
5. Useful in implementing stack-like behavior.
Conclusion
Reversing an array in C is a fundamental programming exercise that strengthens understanding of arrays, loops, recursion, and pointers. The most efficient method is the in-place swapping technique, which runs in O(n) time and O(1) space.
By mastering different approaches such as using loops, recursion, extra arrays, and pointers, programmers gain flexibility and deeper insight into memory and data manipulation in C. This concept serves as a foundation for more advanced topics like sorting algorithms, string manipulation, and data structure implementation.
Codecrown