Program Of Reversing Array Using C
Reverse Array
Array is defined as a contiguous block of memory which allocates similar type of data. In Array indexing starts with 0 and ends with -1.
In reverse array , we traverse elements from end to start.
How to Reverse An Array in C :
In general, Array can be reversed without using in-built method is loop. By using loop we can reverse the array easily.
Below are the two techniques to reverse the array using for loop :
Method 1 :
In the below program, we traverse the string using for loop from back and print the particular character of the string to get the reverse string.
#include<stdio.h> int main () { char s[9] = { 'a', 't', 's', 'n', 'i', 'p', 'e', 'r', 'P' }; // declaration of Array printf ("The reverse array is : "); for (char i = 8; i >= 0; i--) // loop to convert the Array { printf ("%c", s[i]); } return 0; }
Output :
The reverse array is : Prepinsta
In the below program, we traverse the string using for loop from start and print the particular character of the string to get the reverse string by using [n-i] to get the particular character from back
Method 2:
#include<stdio.h> int main () { int s[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // declaration of Array printf ("The reverse Array is : "); for (int i = 0; i < 9; i++) { // loop to convert the Array printf ("%d ", s[8 - i]); } return 0; }
Output :
The reverse Array is : 9 8 7 6 5 4 3 2 1
For more related content : Click-Here
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment