Copy in a reverse manner in c 

07 Sep 2022 Balmiki Mandal 0 C Programming

C Programming: Copy in Reverse

Copying in a reverse manner typically means taking the contents of a source object or data structure and creating a new object or data structure that contains the same elements but in reverse order.

For example, if you have a list of integers [1, 2, 3, 4, 5], copying it in a reverse manner would result in a new list [5, 4, 3, 2, 1].

write a c program to Copy in a reverse manner

#include
int main()
{
    int a[5],b[5],i,j,ele;
    ele=sizeof(a)/sizeof(a[0]);
    printf("Enter the array Elements:");
    for(i=0;i<ele;i++)//this for loop for Scanning the loop
    scanf("%d",&a[i]);
    printf("Array is :");
    for(i=0;i<ele;i++)//This for orinting the loop
    printf(" %d",a[i]);
    printf("\n");
    printf("Reverse array is:");
    for(i=0,j=ele-1;i<ele;i++,j--)//This for loop for reversing the array
    b[j]=a[i];
    for(i=0;i<ele;i++)//printing for reverse array
    printf(" %d",b[i]);
    printf("\n");
}

Output:

Enter the array Elements:10 20 30 40 50

Array is : 10 20 30 40 50
Reverse array is: 50 40 30 20

write a c program to Copy in a reverse manner using the function

#include 

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[5];
    int n = 5;
    for (int i = 0; i < n; i++) {
        arr2[i] = arr1[n-1-i];
    }
    printf("Elements of arr2 are: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr2[i]);
    }
    return 0;
}

Conclusion

In this program, we first define two arrays, arr1, and arr2. We then define a loop that iterates over the elements of arr1, and assigns each element to the corresponding index in arr2 in reverse order. We do this by using the expression arr1[n-1-i] to access the elements of arr1 in reverse order. Finally, we print out the elements of arr2 to verify that the copy operation was successful.

Note that we use the expression n-1-i to access the elements of arr1 in reverse order. The n-1 part is used to get the index of the last element in arr1, and we subtract i from it to get the index of the i-th element from the end of the array. This allows us to copy the elements of arr1 to arr2 in reverse order.

Also, note that we assume that the size of arr1 and arr2 is the same, so we use the same value of n for both arrays. If the size of the two arrays is different, we would need to adjust the loop accordingly.

 

Further Reading:


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

Write a one-line code to copy the string into the destination.

Copy the one array elements to another array in c

Write a program to copy the source string into the destination string in c

Design a function to copy to copy string into destination string using strcpy and strncpy

Design a recursive function to copy the data source string to destination string

Write a c program to copy data one structure to another structure

Implementation of your own copy command in c

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.