reverse for loop c++ code example
Example 1: for loop reverse C++
for (auto it = s.crbegin() ; it != s.crend(); ++it) {
std::cout << *it;
}
for (int i = s.size() - 1; i >= 0; i--) {
std::cout << s[i];
}
Example 2: reverse an array in c++ using while loop
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 9;
int arr [SIZE];
cout << "Enter numbers: \n";
for (int i = 0; i < SIZE; i++)
cin >> arr[i];
for (int i = 0; i < SIZE; i++)
cout << arr[i] << "\t";
cout << endl;
cout << "Reversed Array:\n";
int temp, start = 0, end = SIZE-1;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
for (int i = 0; i < SIZE; i++)
cout << arr[i] << "\t";
cout << endl;
}