write a c program to copy one array element to another array element in reverse manner.

27 Dec 2022 Balmiki Mandal 0 C Programming

Copy Array Elements in Reverse Order using C 

"Write a C program to copy one array element to another array element in a reverse manner" means that you need to write a program in the C language that takes an input array, creates an output array of the same length, and copies the elements of the input array into the output array in reverse order.

For example, if the input array contains the values [1, 2, 3, 4, 5], then the output array should contain the values [5, 4, 3, 2, 1]. The program should be able to handle input arrays of any length and copy the elements in reverse order without modifying the input array.

To accomplish this, you can use a loop to iterate over the input array and copy each element into the corresponding position in the output array in reverse order. Once the loop has completed, the output array will contain a reversed copy of the input array.

C language that copies the elements of one array to another array in reverse order:

#include<stdio.h> 
int main() {
    // Initialize the input array
    int input_arr[] = {1, 2, 3, 4, 5};
    int length = sizeof(input_arr) / sizeof(int);
    
    // Initialize the output array with the same length as the input array
    int output_arr[length];
    
    // Copy the elements from the input array to the output array in reverse order
    for (int i = 0; i < length; i++) {
        output_arr[length - i - 1] = input_arr[i];
    }
    
    // Print the input and output arrays
    printf("Input Array: ");
    for (int i = 0; i < length; i++) {
        printf("%d ", input_arr[i]);
    }
    printf("\n");
    
    printf("Output Array: ");
    for (int i = 0; i < length; i++) {
        printf("%d ", output_arr[i]);
    }
    printf("\n");
    
    return 0;
}

This program initializes an input array with some values, initializes an output array with the same length as the input array, and then uses a for loop to copy the elements from the input array to the output array in reverse order. The program then prints both arrays to the console to show the result.

The output of this program would be:

Input Array: 1 2 3 4 5 
Output Array: 5 4 3 2 1 

Note that in C, we need to declare the length of the array explicitly using sizeof operator to calculate the length of the array in bytes and then divide it by the size of an element.

Top Resources


Copy in a reverse manner in c

Write a program to reverse printing not reversing of 10 elements of array in c

Write a program to reverse the bits of a given number using for loop

Design a function to reverse the given string

Design a function to reverse the word in given string

write a program to copy one array element to another array element in reverse manner.

write a program to reverse adjacent elements of an array.

write a program to reverse half of the elements of an array.

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.