Skip to main content
St Louis

Back to all posts

How to Reverse an Array In C++?

Published on
5 min read
How to Reverse an Array In C++? image

Best Books on C++ Programming to Buy in October 2025

1 C++ Programming Language, The

C++ Programming Language, The

  • QUALITY ASSURANCE: GUARANTEED GOOD CONDITION FOR WORRY-FREE BUYING.
  • AFFORDABLE PRICING: SAVE ON COSTS COMPARED TO NEW BOOKS-GREAT DEALS!
  • ECO-FRIENDLY CHOICE: PROMOTE SUSTAINABILITY BY BUYING USED BOOKS.
BUY & SAVE
$76.10 $89.99
Save 15%
C++ Programming Language, The
2 Beginning C++ Game Programming: Learn C++ from scratch by building fun games

Beginning C++ Game Programming: Learn C++ from scratch by building fun games

BUY & SAVE
$29.99 $49.99
Save 40%
Beginning C++ Game Programming: Learn C++ from scratch by building fun games
3 Programming: Principles and Practice Using C++ (C++ In-depth)

Programming: Principles and Practice Using C++ (C++ In-depth)

BUY & SAVE
$79.99
Programming: Principles and Practice Using C++ (C++ In-depth)
4 C++ Primer (5th Edition)

C++ Primer (5th Edition)

BUY & SAVE
$48.22 $69.99
Save 31%
C++ Primer (5th Edition)
5 C++ All-in-One For Dummies

C++ All-in-One For Dummies

BUY & SAVE
$26.62 $44.99
Save 41%
C++ All-in-One For Dummies
6 Modern C++ Programming Cookbook: Master Modern C++ with comprehensive solutions for C++23 and all previous standards

Modern C++ Programming Cookbook: Master Modern C++ with comprehensive solutions for C++23 and all previous standards

BUY & SAVE
$25.55 $54.99
Save 54%
Modern C++ Programming Cookbook: Master Modern C++ with comprehensive solutions for C++23 and all previous standards
7 C++: The Comprehensive Guide to Mastering Modern C++ from Basics to Advanced Concepts with Hands-on Examples, and Best Practices for Writing Efficient, Secure, and Scalable Code (Rheinwerk Computing)

C++: The Comprehensive Guide to Mastering Modern C++ from Basics to Advanced Concepts with Hands-on Examples, and Best Practices for Writing Efficient, Secure, and Scalable Code (Rheinwerk Computing)

BUY & SAVE
$54.18 $69.95
Save 23%
C++: The Comprehensive Guide to Mastering Modern C++ from Basics to Advanced Concepts with Hands-on Examples, and Best Practices for Writing Efficient, Secure, and Scalable Code (Rheinwerk Computing)
8 Learn C++ Quickly: A Complete Beginner’s Guide to Learning C++, Even If You’re New to Programming (Crash Course With Hands-On Project)

Learn C++ Quickly: A Complete Beginner’s Guide to Learning C++, Even If You’re New to Programming (Crash Course With Hands-On Project)

BUY & SAVE
$14.21 $23.95
Save 41%
Learn C++ Quickly: A Complete Beginner’s Guide to Learning C++, Even If You’re New to Programming (Crash Course With Hands-On Project)
9 C++ Crash Course: A Fast-Paced Introduction

C++ Crash Course: A Fast-Paced Introduction

  • FAST-PACED LEARNING FOR QUICK MASTERY OF C++ CONCEPTS.
  • ENGAGING CONTENT IN CLEAR ENGLISH FOR EASY UNDERSTANDING.
  • DURABLE PAPERBACK FORMAT FOR LASTING USE AND PORTABILITY.
BUY & SAVE
$41.00 $59.99
Save 32%
C++ Crash Course: A Fast-Paced Introduction
10 Tour of C++, A (C++ In-Depth Series)

Tour of C++, A (C++ In-Depth Series)

BUY & SAVE
$33.63 $39.99
Save 16%
Tour of C++, A (C++ In-Depth Series)
+
ONE MORE?

To reverse an array in C++, you can use the following method:

  1. Declare an array of the desired type and size.
  2. Calculate the length of the array using the sizeof() operator.
  3. Declare two variables, start and end, to keep track of the start and end indices of the array.
  4. Use a while loop where start is less than end: Swap the elements at start and end indices of the array. Increment start and decrement end.
  5. The array will be reversed after completing the loop.

Here's an example implementation:

#include

using namespace std;

void reverseArray(int arr[], int length) { int start = 0; int end = length - 1;

while (start < end) {
    // Swap elements at start and end indices
    int temp = arr\[start\];
    arr\[start\] = arr\[end\];
    arr\[end\] = temp;

    // Increment start and decrement end
    start++;
    end--;
}

}

int main() { int arr[] = {1, 2, 3, 4, 5}; int length = sizeof(arr) / sizeof(arr[0]);

cout << "Original Array: ";
for (int i = 0; i < length; i++) {
    cout << arr\[i\] << " ";
}

reverseArray(arr, length);

cout << "\\nReversed Array: ";
for (int i = 0; i < length; i++) {
    cout << arr\[i\] << " ";
}

return 0;

}

In this example, the reverseArray() function takes an array and its length as parameters. It uses a while loop to swap the elements from the start and end indices until they meet in the middle. The main() function initializes an array, calculates its length, and then calls reverseArray() to reverse the array. Finally, it prints the original and reversed arrays.

How to reverse words in a sentence using an array in C++?

To reverse words in a sentence using an array in C++, you can follow the steps below:

  1. Include the necessary header files:

#include #include using namespace std;

  1. Define a function to reverse the words in a sentence:

void reverseWords(string &sentence) { int len = sentence.length();

// Reverse the entire sentence using two pointers approach int start = 0, end = len - 1; while (start < end) { swap(sentence[start], sentence[end]); start++; end--; }

// Reverse individual words in the sentence using two pointers approach int wordStart = 0; for (int i = 0; i < len; i++) { if (sentence[i] == ' ') { int wordEnd = i - 1; while (wordStart < wordEnd) { swap(sentence[wordStart], sentence[wordEnd]); wordStart++; wordEnd--; } wordStart = i + 1; } }

// Reverse the last word in the sentence (if any) int wordEnd = len - 1; while (wordStart < wordEnd) { swap(sentence[wordStart], sentence[wordEnd]); wordStart++; wordEnd--; } }

  1. Create a main function and utilize the reverseWords function:

int main() { string sentence;

// Read the sentence from the user cout << "Enter a sentence: "; getline(cin, sentence);

// Reverse the words in the sentence reverseWords(sentence);

// Display the reversed sentence cout << "Reversed sentence: " << sentence << endl;

return 0; }

  1. Compile and run the program.

Now, you can enter a sentence and the program will reverse its words. For example:

Enter a sentence: Hello world! Reversed sentence: world! Hello

How to reverse an array by swapping elements in C++?

To reverse an array by swapping elements in C++, you can use the following approach:

#include

void reverseArray(int arr[], int start, int end) { while (start < end) { // Swapping elements int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp;

    start++;
    end--;
}

}

int main() { int size; std::cout << "Enter the size of the array: "; std::cin >> size;

int arr\[size\];
std::cout << "Enter the elements of the array: ";
for (int i = 0; i < size; i++) {
    std::cin >> arr\[i\];
}

std::cout << "Original array: ";
for (int i = 0; i < size; i++) {
    std::cout << arr\[i\] << " ";
}

reverseArray(arr, 0, size - 1);

std::cout << "\\nReversed array: ";
for (int i = 0; i < size; i++) {
    std::cout << arr\[i\] << " ";
}

return 0;

}

This program asks the user to enter the size and elements of an array. It then calls the reverseArray function with the array, starting index, and ending index as arguments. Inside the reverseArray function, a while loop is used to swap the elements at the start and end indices. The loop continues until the start index becomes greater than or equal to the end index, ensuring that all elements are swapped. Finally, the reversed array is printed in the main function.

How to reverse an array of strings in C++?

To reverse an array of strings in C++, you can follow these steps:

  1. Include the necessary header file for input and output () and for string manipulation ().
  2. Create a function named reverseArray that takes an array of strings as a parameter.
  3. Inside the function, declare two integer variables named start and end. Initialize start with 0 and end with the length of the array minus 1.
  4. Use a loop to swap the strings at the start and end indices while incrementing start and decrementing end. Continue the loop until start becomes greater than or equal to end.
  5. After the loop, the array will be reversed.

Here is the complete example code:

#include #include

void reverseArray(std::string arr[], int length) { int start = 0; int end = length - 1;

while (start < end) {
    std::swap(arr\[start\], arr\[end\]);
    start++;
    end--;
}

}

int main() { std::string arr[] = { "Hello", "World", "This", "Is", "Reversed" }; int length = sizeof(arr) / sizeof(arr[0]);

reverseArray(arr, length);

for (int i = 0; i < length; i++) {
    std::cout << arr\[i\] << " ";
}

return 0;

}

This code will reverse the strings in the array "arr" and output the reversed array: "Reversed Is This World Hello".