C Program to Reverse an Array

«
»

Reversing an array means substituting the last element in the first position and vice versa and doing such a thing for all elements of the array. For example, first element is swapped with last, second element is swapped by second last and so on.

Such arrays where the original and reversed arrays are equal are called palindrome arrays.

Examples:

Input array: [1,2,3,4]
Reversed array: [4,3,2,1]

Input array: [3,2,1]
Reversed array: [1,2,3]

Problem Description

Write a program to reverse an array in C.

Problem Solution

Algorithm to Reverse an Array:

advertisement
advertisement

1. Input size of the array and array elements.
2. Declare two variables pointing to the start and end of array
3. Swap the elements at their places and increment variables pointing to the first element and decrement the variable pointing to the last element.
4. Do it until the first variable is less than the second one.
5. Print the Reversed Array.

There are several ways to reverse an array in C language. Let’s take a detailed look at all the approaches to reverse an array in C.

Method 1: Reverse an Array in C using Loops (Naive Approach)

In this approach, we have one variable pointing at the first index and one at last. In each step we swap both elements, increment the first variable and decrement the second variable until the first is less than the second one.

Participate in C Programming Certification Contest of the Month Now!

Example:

Input array: [7,2,4,1,6,4]
Reversed array: [4,6,1,4,2,7]

Start: 0      End: 5        Array After Swapping: [4,2,4,1,6,7]
Incrementing Start and Decrementing End.
 
Start: 1      End: 4        Array After Swapping: [4,6,4,1,2,7]
Incrementing Start and Decrementing End.
 
Start: 2      End: 3        Array After Swapping: [4,6,1,4,2,7]
 
Now after Incrementing Start and Decrementing End, start is greater than end, 
so stopping here and hence, the reversed array is:
Reversed Array: [4,6,1,4,2,7]
Program/Source Code

Here is the source code of the C program to reverse an array using loops. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

/* 
 *  C program to reverse an array using loops
 */
#include <stdio.h>
 
int main()
{
    int size;
    printf("Enter size of the array: ");
    scanf("%d",&size);
    printf("Enter Array Elements: ");
    int arr[size];
 
    //Input array elements
    for(int i=0;i<size;i++)
    scanf("%d",&arr[i]);
    printf("Entered Array is: ");
    for(int i=0;i<size;i++)
    printf("%d ",arr[i]);
 
    //Start points at the first element and end points at the last element
    int start=0,end=size-1;
    while(start<end)
    {
        //Swapping elements
        int temp=arr[start];
        arr[start]=arr[end];
        arr[end]=temp;
 
        //Incrementing start  and decrementing end
        start++;
        end--;
    }
 
    //Printing reversed array
    printf("\nReversed array is: ");
    for(int i=0;i<size;i++)
    printf("%d ",arr[i]);
    return 0;
}
Program Explanation

1. First, input the size of the array.
2. Declare an array and input its elements, then initialize two variables pointing to the first and last index of the array.
3. Swap elements at both indices and increment the first variable and decrement the second variable.
4. Do it until the first variable is less than the second one.
5. Print the reversed array.

advertisement

Time Complexity: O(n)
In the program, we are traversing the array so time complexity is O(n).

Space Complexity: O(n)
Space is required to store the array, so space complexity is O(n).

Program Output

In this case, we enter the size of the array as “6” and the array elements are 7, 2, 4, 1, 6, and 4 as input.

Enter size of the array: 6
Enter Array Elements: 7 2 4 1 6 4
Entered Array is: 7 2 4 1 6 4
Reversed array is: 4 6 1 4 2 7

advertisement
Method 2: Reverse an Array in C using Recursion

In Recursive Approach, we swap the element at index i with element at index (n-i-1), where n is the size of the array, until i reaches the middle of the array.

Example:

Input array: [7,2,4,1]

Initially i=0; (Elements at index i and index n-i-1 are swapped half array is traversed)

Swap Elements at 0 and 3, So the array becomes [1,2,4,7]
Swap Elements at 1 and 2, So the array becomes [1,4,2,7]

So, reversed array is [1,4,2,7]

Program/Source Code

Here is the source code of the C program to reverse an array using recursion. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

/* 
 *  C program to reverse an array using recursion
 */
 
#include <stdio.h>
int * reverse(int start,int mid,int n, int arr[])
{
    // Base Condition
    if(start==mid)
    return arr;
    int temp=arr[start];
    arr[start]=arr[n-1-start];
    arr[n-1-start]=temp;
 
    // Recursive Calls
    return reverse(start+1,mid,n,arr);
}
int main()
{
    int size;
    printf("Enter size of the array: ");
    scanf("%d",&size);
    printf("Enter Array Elements: ");
    int arr[size];
 
    //Input array elements
    for(int i=0;i<size;i++)
    scanf("%d",&arr[i]);
    printf("Entered Array is: ");
    for(int i=0;i<size;i++)
    printf("%d ",arr[i]);
 
    //Start points at the first element and end points at the last element
    int start=0,mid=size/2;
    int *rev=reverse(start,mid,size,arr);
 
    //Printing reversed array
    printf("\nReversed array is: ");
    for(int i=0;i<size;i++)
    printf("%d ",rev[i]);
 
    return 0;
}
Program Explanation

1. First, input the size of the array
2. Declare the array and input its elements.
3. Declare a function to reverse an array which recursively calls itself to reverse the array with a base condition to stop when half of the array is traversed.
4. Call the function or further reversal of elements.
5. Print the reversed array.

Time Complexity: O(n)
In the program, we are traversing the array, so the time complexity is O(n).

Space Complexity: O(n)
Space is required to store the original array and the reversed array, so space complexity is O(n).

Program Output

In this case, we enter the size of the array as “4” and the array elements are 7, 2, 4, and 1 as input.

 
Enter size of the array: 4
Enter Array Elements: 7 2 4 1
Entered Array is: 7 2 4 1
Reversed array is: 1 4 2 7

Method 3: Reverse an Array in C using Function

In this approach, we declare a function in which we swap an array with an approach similar to the naive one and then simply return that array.

Example:

Input array: [6,5,4,3,2,1]
Reversed array: [1,2,3,4,5,6]

Start :0      End:5        Array After Swapping : [1,5,4,3,2,6] 
Incrementing Start And Decrementing End.
 
Start :1      End:4        Array After Swapping : [1,2,4,3,5,6] 
Incrementing Start And Decrementing End.
 
Start :2      End:3        Array After Swapping : [1,2,3,4,5,6] 
Now after Incrementing Start and Decrementing End, start is greater than end,
so stopping here and hence, the reversed array is: 
 
Reversed Array: [1,2,3,4,5,6]
Program/Source Code

Here is the source code of the C program to reverse an array using function. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

/* 
 *  C program to reverse an array using function
 */
 
#include <stdio.h>
int* reverse(int start,int end,int arr[])
{
    // Start variable points at the start of the array
    // End Variables points at the last index of the array
    while(start<end)
    {
        //Swapping elements at start and end
        int temp=arr[start];
        arr[start]=arr[end];
        arr[end]=temp;
 
        //Incrementing start and decrementing end
        start++;
        end--;
    }
    //Returning reversed array
    return arr;
}
int main()
{
    int size;
    printf("Enter size of the array: ");
    scanf("%d",&size);
    printf("Enter Array Elements: ");
    int arr[size];
 
    //Input array elements
    for(int i=0;i<size;i++)
    scanf("%d",&arr[i]);
    printf("Entered Array is: ");
    for(int i=0;i<size;i++)
    printf("%d ",arr[i]);
 
    //Start points at the first element and end points at the last element
    int start=0,end=size-1;
 
    //Printing reversed array
    int *rev=reverse(start,end,arr);
    printf("\nReversed array is: ");
    for(int i=0;i<size;i++)
    printf("%d ",rev[i]);
 
    return 0;
}
Program Explanation

1. First input the size of the array.
2. Declare the array and input its elements.
3. Declare a function to reverse an array which swaps the element with the corresponding element (Ideology is the same as the naive approach).
4. Call the function.
5. Print the reversed array.

Time Complexity: O(n)
In the program, we are traversing the array, so the time complexity is O(n).

Space Complexity: O(n)
Space is required to store the original array and the reversed array, so space complexity is O(n).

Program Output

In this case, we enter the size of the array as “6” and the array elements are 6, 5, 4, 3, 2, and 1 as input.

Enter size of the array: 6
Enter Array Elements: 6 5 4 3 2 1
Entered Array is: 6 5 4 3 2 1
Reversed array is: 1 2 3 4 5 6

Method 4: Reverse an Array in C using Pointers

In this approach, we reverse the array using the pointers. First pointer points at the start of the array and second at the last and in each iteration we swap the two elements and increment and decrement respectively.

Example:

Input array: [2,4,1,6]

Start Points at 2 and last points at 6, swapping both elements
So Array becomes [6,4,1,2]. Incrementing Start and Decrementing Last

Start Points at 1 and last points at 4, swapping both elements
So Array becomes [6,1,4,2].

So the reversed array is : [6,1,4,2]

Program/Source Code

Here is the source code of the C program to reverse an array using pointers. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

/* 
 *  C program to reverse an array using pointers
 */
 
#include <stdio.h>
int *rev_array(int arr[],int n)
{
    //start points to start, end points to last & temp is for swapping two elements
    int *start,*end,temp;
    start=arr;
    end=arr;
 
    //pointing end to last index
    for(int i=0;i<n-1;i++)
    end++;
    for(int i=0;i<n/2;i++)
    {
        //Swapping elements at start and end places
        temp=*start;
        *start=*end;
        *end=temp;
 
        //Incrementing start and decrementing end elements
        start++;
        end--;
    }
    return arr;
}
int main()
{
    int size;
    printf("Enter size of the array: ");
    scanf("%d",&size);
    printf("Enter Array Elements: ");
    int arr[size];
 
    //Input array elements
    for(int i=0;i<size;i++)
    scanf("%d",&arr[i]);
    printf("Entered Array is: ");
    for(int i=0;i<size;i++)
    printf("%d ",arr[i]);
    int *rev=rev_array(arr,size);
 
    //Printing reversed array
    printf("\nReversed array is: ");
    for(int i=0;i<size;i++)
    printf("%d ",rev[i]);
 
    return 0;
}
Program Explanation

1. First, input the size of the array.
2. Declare the array and input array elements.
3. Declare a function to reverse an array which takes an array and its size as a parameter.

  • Declare two pointers start and end.
  • Initially Start and end both points at the first index of array.
  • Iterate end to end of the array.
  • Now swap characters at start and end until the start is less than the end.
  • Return the reversed array.

4. Call the function.
5. Print the reversed array.

Time Complexity: O(n)
In the program, we are traversing the array, so the time complexity is O(n).

Space Complexity: O(n)
Space is required to store the original array and the reversed array, so space complexity is O(n).

Program Output

In this case, we enter the size of the array as “4” and the array elements are 2, 4, 1, and 6 as input.

Enter size of the array: 4
Enter Array Elements: 2 4 1 6
Entered Array is: 2 4 1 6
Reversed array is: 6 1 4 2

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.