- How to reverse an array in C
- Two methods: using another array and in-place reversal
- How to swap elements using a temporary variable
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to reverse an array.
Reversing an array means changing the order of elements so that the first element becomes the last, the second becomes the second last, and so on. It is used in many real-world applications, such as:
- Reversing a string (which is an array of characters)
- Processing data in reverse order
- Undoing operations in data structures
- Palindrome checking algorithms
💡 Key Point: There are two common ways to reverse an array: 1) Using another array and 2) In-place reversal (without using extra space).
C Program to Reverse an Array (Method 1: Using Another Array)
#include <stdio.h>
int main() {
int n, i;
// Ask user for number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Declare arrays
int arr[n];
int reversed[n];
// Read elements into the array
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Reverse the array by copying in reverse order
for(i = 0; i < n; i++) {
reversed[i] = arr[n - 1 - i];
}
// Display original array
printf("\nOriginal array: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Display reversed array
printf("\nReversed array: ");
for(i = 0; i < n; i++) {
printf("%d ", reversed[i]);
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 5 Enter 5 elements: 10 20 30 40 50 Original array: 10 20 30 40 50 Reversed array: 50 40 30 20 10
Another Example:
Enter the number of elements: 4 Enter 4 elements: 5 15 25 35 Original array: 5 15 25 35 Reversed array: 35 25 15 5
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n;— stores the number of elementsint i;— loop counter
- Declare Arrays:
int arr[n];— the original arrayint reversed[n];— the reversed array
- Get User Input: Prompts the user to enter the number of elements and then reads them into the array.
- Reverse the Array: The
forloop copies elements fromarrtoreversedin reverse order:reversed[i] = arr[n - 1 - i];- When
i = 0, copies the last element to the first position - When
i = 1, copies the second last element to the second position - And so on...
- Display Results: Prints both the original and reversed arrays.
- Return:
return 0;indicates successful program execution.
📝 Note: This method uses extra space (another array) of the same size. For large arrays, this consumes additional memory.
Method 2: In-Place Reversal (More Efficient)
The in-place reversal method does not use extra space. It swaps elements from both ends of the array until the middle is reached.
#include <stdio.h>
int main() {
int n, i, temp;
printf("Enter the 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]);
}
// In-place reversal using two pointers
for(i = 0; i < n/2; i++) {
// Swap arr[i] with arr[n-1-i]
temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
// Display reversed array
printf("\nReversed array: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Sample Output:
Enter the number of elements: 6 Enter 6 elements: 10 20 30 40 50 60 Reversed array: 60 50 40 30 20 10
How In-Place Reversal Works
The algorithm uses two pointers:
- Pointer 1: Starts from the beginning of the array (
i) - Pointer 2: Starts from the end of the array (
n - 1 - i) - Swap the elements at these two positions
- Move pointer 1 forward and pointer 2 backward
- Stop when the pointers meet or cross (i < n/2)
💡 Visual Example:
Original: [10, 20, 30, 40, 50]
Step 1: Swap 10 and 50 → [50, 20, 30, 40, 10]
Step 2: Swap 20 and 40 → [50, 40, 30, 20, 10]
Step 3: Middle element (30) stays in place
Result: [50, 40, 30, 20, 10]
Algorithm for In-Place Reversal
- Start
- Read the number of elements (n)
- Read n elements into the array arr
- For
i = 0ton/2 - 1:- Set
temp = arr[i] - Set
arr[i] = arr[n - 1 - i] - Set
arr[n - 1 - i] = temp
- Set
- Print the reversed array
- End
💻 Practice Exercise
Challenge 1: Write a program to reverse only the first half of the array.
Challenge 2: Reverse the array using recursion.
🔍 Click to Show Solution for Challenge 2
#include <stdio.h>
void reverseArray(int arr[], int start, int end) {
// Base case: if start is greater than or equal to end
if(start >= end) {
return;
}
// Swap elements at start and end
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// Recursively reverse the remaining array
reverseArray(arr, start + 1, end - 1);
}
int main() {
int n, i;
printf("Enter the 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]);
}
// Call recursive function to reverse
reverseArray(arr, 0, n - 1);
printf("\nReversed array: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Frequently Asked Questions
1. How do you reverse an array in C?
You can reverse an array using two methods: 1) Copying to another array in reverse order, or 2) In-place reversal by swapping elements from both ends.
2. Which method is better for reversing an array?
The in-place reversal method is better because it uses O(1) extra space, while the other method uses O(n) extra space. Both have O(n) time complexity.
3. What is the time complexity of array reversal?
Both methods have a time complexity of O(n), where n is the number of elements in the array.
4. Can I reverse a string using the same method?
Yes! A string is essentially an array of characters. You can use the same in-place reversal method to reverse a string.
5. What happens if the array has only one element?
If the array has only one element, reversing it does nothing. The loop condition i < n/2 fails (1/2 = 0), so no swaps are performed.
💡 Tip: In-place reversal is more memory-efficient and is preferred in most real-world applications.