Program to copy the contents of one array into another in the reverse order
Given an array, the task is to copy these array elements into another array in reverse array.
Examples:
Input: array: 1 2 3 4 5 Output: 5 4 3 2 1 Input: array: 10 20 30 40 50 Output: 50 40 30 20 10
Let len be the length of original array. We copy every element original_arr[i] to copy_arr[n-i-1] to get reverse in copy_arr[].
C++
// C program to copy the contents// of one array into another// in the reverse order#include <stdio.h>// Function to print the arrayvoid printArray(int arr[], int len){ int i; for (i = 0; i < len; i++) { printf("%d ", arr[i]); }}// Driver codeint main(){ int original_arr[] = {1, 2, 3, 4, 5}; int len = sizeof(original_arr)/sizeof(original_arr[0]); int copied_arr[len], i, j; // Copy the elements of the array // in the copied_arr in Reverse Order for (i = 0; i < len; i++) { copied_arr[i] = original_arr[len - i - 1]; } // Print the original_arr printf("\nOriginal array: "); printArray(original_arr, len); // Print the copied array printf("\nResultant array: "); printArray(copied_arr, len); return 0;} |
Java
// Java program to copy the contents// of one array into another// in the reverse orderclass GFG {// Function to print the arraystatic void printArray(int arr[], int len){ int i; for (i = 0; i < len; i++) { System.out.printf("%d ", arr[i]); }}// Driver codepublic static void main(String[] args) { int original_arr[] = {1, 2, 3, 4, 5}; int len = original_arr.length; int copied_arr[] = new int[len], i, j; // Copy the elements of the array // in the copied_arr in Reverse Order for (i = 0; i < len; i++) { copied_arr[i] = original_arr[len - i - 1]; } // Print the original_arr System.out.printf("\nOriginal array: "); printArray(original_arr, len); // Print the copied array System.out.printf("\nResultant array: "); printArray(copied_arr, len); }}// This code is contributed by // PrinciRaj1992 |
Python3
# Python3 program to copy the contents of one# array into another in the reverse orderimport math as mt# Function to print the arraydef printArray(arr, Len): for i in range(Len): print(arr[i], end = " ") # Driver codeoriginal_arr = [1, 2, 3, 4, 5]Len = len(original_arr)copied_arr = [0 for i in range(Len)]# Copy the elements of the array# in the copied_arr in Reverse Orderfor i in range(Len): copied_arr[i] = original_arr[Len - i - 1]# Print the original_arrprint("Original array: ", end = "")printArray(original_arr, Len)# Print the copied arrayprint("\nResultant array: ", end = "")printArray(copied_arr, Len)# This code is contributed by# Mohit kumar 29 |
C#
// C# program to copy the contents // of one array into another // in the reverse orderusing System;class GFG { // Function to print the array static void printArray(int []arr, int len) { int i; for (i = 0; i < len; i++) { Console.Write(arr[i]); } } // Driver code public static void Main() { int []original_arr = {1, 2, 3, 4, 5}; int len = original_arr.Length; int []copied_arr = new int[len]; int i; // Copy the elements of the array // in the copied_arr in Reverse Order for (i = 0; i < len; i++) { copied_arr[i] = original_arr[len - i - 1]; } // Print the original_arr Console.Write("\nOriginal array: "); printArray(original_arr, len); // Print the copied array Console.Write("\nResultant array: "); printArray(copied_arr, len); } } // This code is contributed by Rajput-Ji |
PHP
<?php// PHP program to copy the contents // of one array into another // in the reverse order // Function to print the array function printArray($arr, $len) { for ($i = 0; $i < $len; $i++) { echo $arr[$i], " "; } } // Driver code $original_arr = array(1, 2, 3, 4, 5); $len = sizeof($original_arr);$copied_arr = array();// Copy the elements of the array // in the copied_arr in Reverse Order for ($i = 0; $i < $len; $i++) { $copied_arr[$i] = $original_arr[$len - $i - 1]; } // Print the original_arr echo "Original array: "; printArray($original_arr, $len);// Print the copied array echo "\nResultant array: "; printArray($copied_arr, $len); // This code is contributed by Ryuga?> |
Output:
Original array: 1 2 3 4 5 Resultant array: 5 4 3 2 1
Time Complexity: O(len)
Auxiliary Space: O(len)
Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

