Reverse array in C
In this post, you will learn how to reverse an array using the C programming language.
Suppose we have an array arr = [12,4,3,6,2,7,3]. We shall have to reverse the elements present in the array and display them. Then the output will be [3,7,2,6,3,4,12].
Iterative way to reverse an array in C
In the given C program, we initialize the start and end indexes. In a while loop, we swap arr[start] with arr[end] and change start and end as start = start +1, end = end – 1.
#include<stdio.h>
/* Function to reverse an array */
void rvereseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
/* function to print an array */
void printArray(int arr[], int size)
{
int x;
for (x=0; x < size; x++)
printf("%d ", arr[x]);
printf("\n");
}
/* Driver function */
int main()
{
int arr[] = {3, 5, 6, 7, 1, 2};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, size);
rvereseArray(arr, 0, size-1);
printf("Reversed array: \n");
printArray(arr, size);
return 0;
}
Output of the above code:
Original array:
3 5 6 7 1 2
Reversed array:
2 1 7 6 5 3
Reverse an array in C using recursion
In the given C program, we define a function reverseArray and initialize the start and end indexes and swap arr[start] with arr[end]. We recursively call this function to reverse the array.
#include<stdio.h>
/* Function to reverse an array */
void reverseArray(int arr[], int start, int end)
{
int temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverseArray(arr, start+1, end-1);
}
/* Function to print out an array */
void printArray(int arr[], int size)
{
int x;
for (x=0; x < size; x++)
printf("%d ", arr[x]);
printf("\n");
}
/* Driver function */
int main()
{
int arr[] = {5,3,2,7,8,1};
printf("Original array: \n");
printArray(arr, 6);
reverseArray(arr, 0, 5);
printf("Reversed array: \n");
printArray(arr, 6);
return 0;
}
Output of the above code:
Original array:
5 3 2 7 8 1
Reversed array:
1 8 7 2 3 5
C program to reverse an array using user input
In the given example, we ask the user to enter the size of an array and elements of an array and then use the while loop to reverse an array.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], reverse[MAX_SIZE];
int size, i, start, end;
/* Getting size of the array */
printf("Enter size of the array: ");
scanf("%d", &size);
/* Getting array elements */
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
end = 0;
start = size - 1;
while(start >= 0)
{
/* Assign value from original array to reverse array */
reverse[end] = arr[start];
end++;
start--;
}
/* Printing the reversed array */
printf("\nReversed array: ");
for(i=0; i<size; i++)
{
printf("%d\t", reverse[i]);
}
return 0;
}
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative